This file contains 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 Main { | |
public static void main(String[] args) { | |
Tree tree = initiateTree(); | |
tree.printTree(); | |
int searchValue = 10; | |
Node resultNode = tree.dfs(searchValue); |
This file contains 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.LinkedList; | |
import java.util.Queue; | |
import java.util.Scanner; | |
/** | |
* Given a list of numbers, write a function that would return | |
* the consecutive sequence of that list that sums up to a specific number. | |
* If no such sequence return -1. | |
Input | |
9 9 |
This file contains 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 CircularRotation { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int k = scanner.nextInt(); | |
int q = scanner.nextInt(); | |
int[] array = new int[n]; | |
for (int i = 0; i < array.length; i++) { | |
array[(i + k) % array.length] = scanner.nextInt(); | |
} |
This file contains 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.*; | |
/** | |
* Identify whether there exists a pair of numbers in an array such that their sum is equal to N | |
*/ | |
public class SumNumbers1 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int m = scanner.nextInt(); |
This file contains 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 an array, find there are 3 numbers have | |
when we add them the value will equals a specified sum | |
Example: | |
{1,4,6,10,20,21} | |
Sum=32, Result:true (1+10+21) | |
Sum=65, Result:false | |
6 32 | |
1 4 6 10 20 21 |
This file contains 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 QuickSelect { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int[] array = new int[n]; | |
for (int i = 0; i < array.length; i++) { | |
array[i] = scanner.nextInt(); | |
} | |
int pos = array.length / 2; | |
int median = quickSelect(array, pos, 0, array.length - 1); |
This file contains 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 BalacedBrackets { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int t = in.nextInt(); | |
for(int a0 = 0; a0 < t; a0++){ | |
String s = in.next(); | |
getResult(s); | |
} | |
} |
This file contains 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 MinPriorityQueue { | |
private List<Integer> list = new ArrayList<>(); | |
public MinPriorityQueue() { | |
} | |
public MinPriorityQueue(int capacity) { | |
list = new ArrayList<>(capacity); | |
} |
This file contains 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
//http://stackoverflow.com/questions/15319561/how-to-implement-a-median-heap | |
class MedianHeap { | |
private PriorityQueue<Integer> minHeap = new PriorityQueue<>(10, new Comparator<Integer>() { | |
@Override | |
public int compare(Integer o1, Integer o2) { | |
return o2 - o1; | |
} | |
}); | |
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>(); |
This file contains 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
/** | |
* | |
* - Take first item of array and place it in position where all elements to left | |
* will be less or equals to the item and the all elements to right will | |
* be greater than the item, so we place it in its final position | |
- Than recursively do the same for the all elements in the left part and all elements in the right part and so on | |
*/ | |
public class QuickSort { | |
public static void main(String[] args) { |
NewerOlder