Skip to content

Instantly share code, notes, and snippets.

@nichtemna
nichtemna / Main.java
Created December 18, 2018 09:34
DFS in Java
public class Main {
public static void main(String[] args) {
Tree tree = initiateTree();
tree.printTree();
int searchValue = 10;
Node resultNode = tree.dfs(searchValue);
@nichtemna
nichtemna / SequenceSum.java
Created September 24, 2016 13:45
Given a list of numbers, write a function that would return the consecutive sequence of that list that sums up to a specific number.
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
@nichtemna
nichtemna / CircularRotation.java
Created September 22, 2016 13:40
CircularRotation of array
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();
}
@nichtemna
nichtemna / SumNumbers1,java
Created September 22, 2016 06:56
Identify whether there exists a pair of numbers in an array such that their sum is equal to N
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();
@nichtemna
nichtemna / SumNumbers.java
Created September 22, 2016 06:12
Sum three numbers of array to get desired sum
/**
* 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
@nichtemna
nichtemna / QuickSelect.java
Created September 20, 2016 15:20
Quick select algorithm searching for median in array
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);
@nichtemna
nichtemna / BalacedBrackets.java
Created September 16, 2016 14:56
Balanced parenthesis Create function that will determine are the parenthesis balanced in a given string
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);
}
}
@nichtemna
nichtemna / MinPriorityQueue.java
Created September 16, 2016 14:03
MinHeap - binary tree(each node has zero, one, or two children) where the value of each nodes at most the values its children.
public class MinPriorityQueue {
private List<Integer> list = new ArrayList<>();
public MinPriorityQueue() {
}
public MinPriorityQueue(int capacity) {
list = new ArrayList<>(capacity);
}
@nichtemna
nichtemna / MedianHeap.java
Created September 16, 2016 14:01
MedianHeap - data structure that returns median of inserted values. Implemented base on two heaps.
//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<>();
@nichtemna
nichtemna / QuickSort.java
Created September 11, 2016 13:20
Quick sort
/**
*
* - 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) {