Skip to content

Instantly share code, notes, and snippets.

@nichtemna
nichtemna / MergeSort.java
Created September 11, 2016 13:06
Merge sort
/**
*
Merge sort O(n log(n)):
- Break array in two parts and sort them recursively
- Merge two sorted array in one resulting array
*/
public class MergeSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
@nichtemna
nichtemna / InsertionSort.java
Created September 11, 2016 11:58
Insertion sort
/**
* Insertion sort O(n2):
- Take i-th element and find it’s place in the part of array that goes before it(this part already sorted)
- Slide all elements in sorted part of array to the right for 1 position and insert the element
- Precede! All elements to left of i-th element a sorted already
*/
public class InsertionSort {
public static void main(String[] args) {
@nichtemna
nichtemna / SelectionSort.java
Created September 11, 2016 11:16
Selection sort
package search_sort;
import java.util.Scanner;
/**
*
Selection sort O(n2):
Find the minimum by scanning the array
Swap it with the first element
Increment index and repeat with the remaining part of the array
@nichtemna
nichtemna / BinarySearch.java
Created September 11, 2016 06:56
Binary search
public class BinarySearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int search = scanner.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
System.out.println(binarySearh(search, array));
@nichtemna
nichtemna / ThreeArray.java
Created September 9, 2016 19:54
Find interception of three array
import java.util.*;
/**
* Given a 3 array like below
NSArray *a = [1,3,4,5];
NSArray *b = [-1,3,0,9];
NSArray *c = [0,31,32,22,6];
Find the elements from the three array which existing in atleast 2 arrays.
@nichtemna
nichtemna / Hourglass.java
Created September 8, 2016 10:40
2D Array - DS
/**
Given a 2D Array, :
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
@nichtemna
nichtemna / GetNextWord.java
Created September 8, 2016 10:36
Hackerrank - Bigger is Greater
// https://www.hackerrank.com/challenges/bigger-is-greater
// Given a word , rearrange the letters of to construct another word in such a way that is lexicographically greater than . In case of multiple possible answers, find the lexicographically smallest one among them.
// Input Format
// The first line of input contains , the number of test cases. Each of the next lines contains .
// Constraints
// will contain only lower-case English letters and its length will not exceed .
// Output Format
@nichtemna
nichtemna / DPKnapsack.java
Last active September 4, 2016 09:17
Knapsack with repetitions
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Given a list of n integers, , and another integer, k representing the expected sum. Select zero or more numbers(as less as possible)
* from such that the sum of these numbers is as near as possible, but not exceeding, to the expected sum (k).
Each element of can be selected multiple times.
If no element is selected then the sum is 0.
@nichtemna
nichtemna / PrimitiveCalculator.java
Created September 2, 2016 16:03
Primitive Calculator
Problem Introduction
You are given a primitive calculator that can perform the following three operations with the current num-
ber x: multiply x by 2, multiply x by 3, or add 1 to x. Your goal is given a positive integer n, find the
minimum number of operations needed to obtain the number n starting from the number 1.
Problem Description
Task. Given an integer n, compute the minimum number of operations needed to obtain the number n
starting from the number 1.
Input Format. The input consists of a single integer 1 ≤ n ≤ 106 .
Output Format. In the first line, output the minimum number k of operations needed to get n from 1.
In the second line output a sequence of intermediate numbers. That is, the second line should contain
@nichtemna
nichtemna / SmallestCommon.java
Created August 29, 2016 14:59
Find the smallest common element in two arrays
import java.util.*;
/**
* Find the smallest common element in two arrays , if no return -1. Algorithm works in a linear time
*/
public class SmallestCommon {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();