This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |