Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
KodeSeeker / FindDuplicateinArray.java
Created February 7, 2014 23:52
Find duplicates in O(n) time and O(1) extra space. Given an array of n elements which contains elements from 0 to n-1
/*
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++)
{
@KodeSeeker
KodeSeeker / SwapPairBits.java
Created February 12, 2014 02:40
Swapping pair of bits in a Byte
/*
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:
@KodeSeeker
KodeSeeker / BinarySearchTreeIterator.java
Created February 21, 2014 03:41
Iterator Binary Search Tree
/* 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) {
@KodeSeeker
KodeSeeker / Power.java
Last active August 29, 2015 13:56
Power function pow(a,b)
/*
Pow(a,b)..
First we use a O(n) solution
pow(a,b)
*/
int power (int a, int b)
{
@KodeSeeker
KodeSeeker / TwoTreesInOrderTraversal.java
Created February 23, 2014 07:58
Write a program to print inorder traversal of two trees. Both values must be printed alternatively. e.g. if inorder traversal of tree 1 is 10, 15, 20 and tree 2 is 100, 150, 200 then it should print 10, 100, 15, 150, 20, 200.
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);
}
@KodeSeeker
KodeSeeker / StringreversalRecursive.java
Created February 27, 2014 05:29
Reverse String recursively
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
@KodeSeeker
KodeSeeker / FindIndexFromWord.java
Last active August 29, 2015 13:56
There is a dictionary of billion words and there is one method provided String getWord(int index); We can give it index and it will return the String on that index . Now word is given to us we have to find out its index. O(logn) solution needed
// 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);
@KodeSeeker
KodeSeeker / CountInversions.java
Created March 4, 2014 02:43
Count the number of inversions in an array
// 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.
@KodeSeeker
KodeSeeker / NGEArray.java
Last active August 29, 2015 13:56
Print the next greatest element pair in an array-- Extra space.
// 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
@KodeSeeker
KodeSeeker / SumFromRootToLeaf.java
Last active August 29, 2015 13:57
Root to leaf path that sums up to a given number.
// 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
{