Skip to content

Instantly share code, notes, and snippets.

View arjunrao87's full-sized avatar
👋

Arjun Rao arjunrao87

👋
View GitHub Profile
@arjunrao87
arjunrao87 / ReverseLinkedList.java
Created December 21, 2014 14:52
Reverse linked list
class Node{
private int data;
private Node next;
public Node( int data ){
this.data = data;
this.next = null;
}
public void setNext( Node next ){
@arjunrao87
arjunrao87 / ReverseKLL.java
Created December 21, 2014 15:36
Reverse each k elements in LL
public void reverseKLL( Node head, int k ){
Node prev;
Node next;
Node current = head;
int count = 0;
while( current != null ){
Node boundaryNode = getBoundaryNode( current );
while( boundaryNode != null && count < k ){
next = current.getNext();
@arjunrao87
arjunrao87 / SumPairOfIntegers.java
Created December 21, 2014 16:16
Given an unsorted array of n integers, find a pair of integers whose sum = k
//Method 1 : Time = O(n), Space = O(1)
public List<Pair<Integer, Integer>> getPairsOfIntegers( int[] numbers, int sum ){
List<Pair<Integer,Integer>> results = new ArrayList<>();
int[] sortedNumbers = doRadixSort( numbers );
int left = 0;
int right = sortedNumbers.length - 1;
while( left < right ){
int currentSum = sortedNumbers[ left ] + sortedNumbers[ right ];
if ( currentSum == sum ){
results.add( new Pair<Integer,Integer>( sortedNumbers[ left ], sortedNumbers[ right ] );
@arjunrao87
arjunrao87 / RemoveVowels.java
Last active August 29, 2015 14:11
In-place removal of vowels from a string
public String removeVowels( String testString ){
char[] s = testString.toCharArray();
int vowelCount = 0;
int vowelStartIndex = -1;
for ( int i = 0; i < s.length; i ++ ){
if( isVowel( s[i] ) ){
if( vowelCount == 0 )
vowelStartIndex = i;
vowelCount++;
@arjunrao87
arjunrao87 / Quicksort.java
Created December 21, 2014 20:24
Quicksort algorithm
public void quickSort(int[] array) {
quickSort(array, 0, array.length - 1);
}
public void quickSort(int[] array, int left, int right) {
int pivotIndex = partition(array, left, right);
if (left < pivotIndex - 1) {
quickSort(array, left, pivotIndex - 1);
}
if (right > pivotIndex) {
@arjunrao87
arjunrao87 / Mergesort.java
Created December 21, 2014 20:56
Mergesort
public void sort( int[] numbers ){
mergesort( numbers );
}
public void mergesort( int[] numbers ){
if( numbers.length > 1 ){
int[] leftHalf = splitLeft( numbers );
int[] rightHalf = splitRight( numbers );
mergesort( leftHalf );
mergesort( rightHalf );
@arjunrao87
arjunrao87 / RootToLeafSum.java
Created December 21, 2014 22:08
Given a tree and a sum, return true if there is a path from the root down to a leaf, such that adding up all the values along the path equals the given sum.
public boolean isSumExists( Node root, int sum ){
if( root == null && sum != 0 ){
return false;
}
int currentNodeSum = sum - root.getData();
if( currentNodeSum == 0 ){
return true;
} else{
return ( isSumExists( root.getLeftChild(), currentNodeSum ) || isSumExists( root.getRightChild(), currentNodeSum ) );
}
@arjunrao87
arjunrao87 / BSTHeight.java
Created December 21, 2014 23:50
Height of a BST
public int height( Node root ){
if( root == null ){
return 0;
}
int heightLeft = height( root.getLeftChild() );
int heightRight = height( root.getRightChild() );
return Math.max( heightLeft, heightRight ) + 1;
}
@arjunrao87
arjunrao87 / LCABinaryTree.java
Created December 22, 2014 00:34
Find least common ancestor for 2 nodes a, b in a binary tree ( not BST )
public Node getLCA( Node node, int a, int b ){
if( node == null ){
return node;
}
if( node.getData() == a || node.getData() == b ){
return node;
}
Node lcaInLeft = getLCA( node.getLeft(), a, b );
@arjunrao87
arjunrao87 / ClosestBSTNode.java
Last active August 29, 2015 14:11
Given a binary search tree and a value k, please find a node in the binary search tree whose value is closest to k.
//Method 1:
public Node getClosestBSTNode( Node root, int k ){
if( root == null ){
return null;
}
return getClosestBSTNode( root, k, root.data() - k );
}
public Node getClosestBSTNode( Node node, int k, int parentDiff ){