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 static void quickSort(int[] a, int p, int r) | |
{ | |
if(p<r) | |
{ | |
int q=partition(a,p,r); | |
quickSort(a,p,q); | |
quickSort(a,q+1,r); | |
} | |
} |
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 static int[] mergeSort(int [] list) { | |
if (list.length <= 1) { | |
return list; | |
} | |
// Split the array in half | |
int[] first = new int[list.length / 2]; | |
int[] second = new int[list.length - first.length]; | |
System.arraycopy(list, 0, first, 0, first.length); | |
System.arraycopy(list, first.length, second, 0, second.length); |
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 static void main(String[] args) { | |
String input = "abcd"; | |
permute("", input); | |
} | |
public static void permute(String prefix, String input) { | |
int m = prefix.length(); | |
if (m >= 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
/** | |
* Write a program to print all the LEADERS in the array. | |
* An element is leader if it is greater than all the elements to its right side. | |
* And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2. | |
*/ | |
void printLeaders(int arr[], int size) | |
{ | |
int max_from_right = arr[size-1]; | |
int i; | |
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 HashEntry { | |
private int key; | |
private int value; | |
HashEntry(int key, int value) { | |
this.key = key; | |
this.value = value; | |
} | |
public int getKey() { |
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.List; | |
public final class FlattenUtil { | |
public static List<Object> flatten(List<?> list) { | |
List<Object> retVal = new LinkedList<Object>(); | |
flatten(list, retVal); | |
return retVal; |
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 Node findLca(Node node, int t1, int t2) { | |
if(node == null) { | |
return null; | |
} | |
if(node.value > t2 && node.value > t1) { | |
// both targets are left | |
return findLca(node.left, t1, t2); | |
} else if (node.value < t2 && node.value < t1) { | |
// both targets are right | |
return findLca(node.right, t1, t2); |
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
Node slow, fast, start; | |
fast = slow = head; | |
//PART I - Detect if a loop exists | |
while (true) | |
{ | |
// fast will always fall off the end of the list if it is linear | |
if (fast == null || fast.next == null) | |
{ | |
// no loop |
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
// Sets the nextRight of root and calls connectRecur() for other nodes | |
void connect (struct node *p) | |
{ | |
// Set the nextRight for root | |
p->nextRight = NULL; | |
// Set the next right for rest of the nodes (other than root) | |
connectRecur(p); | |
} | |
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
// This function prints the longest palindrome substring of str[]. | |
// It also returns the length of the longest palindrome | |
int longestPalSubstr( char *str ) | |
{ | |
int n = strlen( str ); // get length of input string | |
// table[i][j] will be false if substring str[i..j] is not palindrome. | |
// Else table[i][j] will be true | |
bool table[n][n]; | |
memset( table, 0, sizeof( table ) ); |