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
/* | |
Key factor here array is of length n and has elements from 0 to n-1. | |
*/ | |
public void printRepeating(int[] arr) | |
{ | |
int len = arr.length; | |
for(int i=0; i<len;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
/* | |
http://stackoverflow.com/questions/3758402/swapping-pair-of-bits-in-a-byte | |
It works by handling the low bits and high bits of each bit-pair separately and then combining the result: | |
The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position. | |
Similarly the expression (x & 0b01010101) << 1 extracts the low bit from each pair and shifts it to the high bit position. | |
The two parts are then combined using bitwise-OR. | |
Since not all languages allow you to write binary literals directly, you could write them in for example hexadecimal: |
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
/* An iterator that iterates through a tree using in-order tree traversal | |
* allowing a sorted sequence. | |
* | |
*/ | |
public class Iterator { | |
private Stack<Node> stack = new Stack<>(); | |
private Node current; | |
private Iterator(Node argRoot) { |
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
/* | |
Pow(a,b).. | |
First we use a O(n) solution | |
pow(a,b) | |
*/ | |
int power (int a, int b) | |
{ |
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 void alternateTraverse(Node root1, Node root2) { | |
if (root1 != null && root2 != null) { | |
alternateTraverse(root1.left, root2.left); | |
System.out.println("Tree1: " + root1.data); | |
System.out.println("Tree2: " + root2.data); | |
alternateTraverse(root1.right, root2.right); | |
} |
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 String reverse(String str) { | |
if ((null == str) || (str.length() <= 1)) { | |
return str; | |
} | |
return reverse(str.substring(1)) + str.charAt(0); | |
} |
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
// call findIndex(str,0,dict.size(),dict) | |
public static int findIndex(String str,int begin, int end, Map<Integer,String> dict) | |
{ | |
int mid =(begin+end)/2; | |
String midString= dict.get(mid); | |
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
// Count the number of inversions | |
//Trick is to use mergesort--> tweak in merge procedure | |
int merge(int[] arr, int [] left, int [] right,int ) | |
{ | |
int i,j,count; | |
while(i<left.length || j< right.length) | |
{ | |
if(j== right.length)// the right array has been completely looked at. |
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
// Core idea is to use space to improve over the brute force appraoch | |
// Use a stack to store elements < current. | |
public void printNGE(int [] arr) | |
{ | |
int len= arr.length; | |
Stack<Integer> nums= new Stack<Integer>(); | |
if len==0 |
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
// Find if there is a path from the root to the leaf of a tree that sums up to a given parameter. | |
// Idea - Recursive. At each level subtract sum from node value to see if sum==0 and there are no children. | |
public void boolean SumFromRootToLeaf(Node root, int sum) | |
{ | |
if(root == null) | |
sum==0? return true: return false;// sum ==0 and root = null-> exit condition. | |
else | |
{ |
OlderNewer