Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:10570853
Created April 13, 2014 05:45
Deepest left leaf node in a binary tree
public class DeepestLeftLeafNodeFinder {
Node deepestNode;
int deepestNodeDepth;
public Node findDeepestLeftLeafNode(Node root,boolean isLeftNode,int depth){
if(isLeftNode){
if(depth > deepestNodeDepth){
deepestNode = root;
@gabhi
gabhi / gist:10571278
Created April 13, 2014 06:09
Move all zeroes to end of array
// Function which pushes all zeros to end of an array.
void pushZerosToEnd(int arr[], int n)
{
int count = 0; // Count of non-zero elements
// Traverse the array. If element encountered is non-zero, then
// replace the element at index 'count' with this element
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i]; // here count is incremented
@gabhi
gabhi / gist:11232921
Created April 23, 2014 21:24
quicksort vs mergesort
Quicksort can sort "inline" of an existing collection, e.g. it does not have to create a copy of the collection while Mergesort requires a copy.
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.LinkedList;
@gabhi
gabhi / gist:11233655
Last active August 29, 2015 14:00
Graph DFS
/*
Step 1: Push the root node in the Stack.
Step 2: Loop until stack is empty.
Step 3: Peek the node of the stack.
Step 4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and push it on stack.
Step 5: If the node does not have any unvisited child nodes, pop the node from the stack.
*/
public void dfs()
{
//DFS uses Stack data structure
@gabhi
gabhi / gist:11233744
Created April 23, 2014 21:53
Graph BFS
/*
Step 1: Push the root node in the Queue.
Step 2: Loop until the queue is empty.
Step 3: Remove the node from the Queue.
Step 4: If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue.
*/
public void bfs()
{
@gabhi
gabhi / gist:11233979
Created April 23, 2014 22:02
Merge Sort
<T> void mergeSort(T[] a, Comparator<T> c) {
if (a.length <= 1) return;
T[] a0 = Arrays.copyOfRange(a, 0, a.length/2);
T[] a1 = Arrays.copyOfRange(a, a.length/2, a.length);
mergeSort(a0, c);
mergeSort(a1, c);
merge(a0, a1, a, c);
}
@gabhi
gabhi / gist:11242292
Created April 24, 2014 05:14
Find kth smallest element in an array
/*
Find kth smallest element in an array
*/
@gabhi
gabhi / gist:11242633
Created April 24, 2014 05:32
Longest compound word
private boolean isCompoundWord(String word) {
if (dictionary.contains(word)) return true;
for (int i = 1; i < word.length(); i++) {
String prefix = word.substring(0, i);
if (isCompoundWord(prefix)) {
String remainder = word.substring(i, word.length());
if (remainder.length() == 0) return true;
return isCompoundWord(remainder);
@gabhi
gabhi / gist:11242984
Created April 24, 2014 05:52
Largest Continuous Sum of an Array
public Integer findSum(int[] arr) {
if (arr.length == 0)
return null;
int largestSum = arr[0];
int currentSum = arr[0];
for (int i = 1; i < arr.length; i++) {
currentSum = max(currentSum + arr[i], arr[i]); // if the current sum gets negative, we reset it
largestSum = max(currentSum, largestSum);
}