Sunday, September 8, 2013

Remove K nodes from back and add in front in Link List.

WAP such that remove K elements from back and add in front.
Example:
Sample Input : 
Link list :10->20->100->30->50->5->70->234
Given K : 3
Sample Output:
5->70->234->10->20->100->30->50
Algo:
  1. Take two pointer both pointing head at beginning.
  2. Move firstPointer to K node.
  3. Now move both first and second pointer while firstPointer != null.
  4. Here secondPointer will point to Kth Node from Last.
  5. Make node of secondPointer as head of Link list.

Source Code:

Addition of integers given in two linklist.

Problem: You have two numbers represented by a linked list, where each node contains a sin-gle digit The digits are stored in reverse order, such that the 1s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list
EXAMPLE 
Sample Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Sample Output: 8 -> 0 -> 8

Sample Input:
Integer 1: 1 1 5 
Integer 2: 1 9 3 
Sample Output:
Result: 2 0 9

Algorithm:

Source Code:

Reversing a sentence.

Problem: WAP reverse a given sentence word by word.

Sample Input       : India is in Asia.
Sample Output    : Asia in is India.
Algorithm:
  1. Reverse each word.
  2. After that Reverse Full sentence.
Source Code:

Junit Test case for above Program:

Saturday, September 7, 2013

Check is given BST balanced or not.

Algorithm:

  1. Recursively find max height and min height of tree.
  2. Subtract max height-min height.
  3. If result is 0 or 1 then tree is balanced else not.
Source Code:(Java)
package com.tree.exercise;

public class CheckTreeBalanced {

 
 private static int  findMaxHeight(Node root){
  
  if(root==null){
   return 0;
  }
  else
  {
   return 1 + Math.max(findMaxHeight(root.lChild), findMaxHeight(root.rChild));
  }
  
 }
 
private static int  findMinHeight(Node root){
  
  if(root==null){
   return 0;
  }
  else
  {
   return 1 + Math.min(findMaxHeight(root.lChild), findMaxHeight(root.rChild));
  }
  
 }

public static boolean isBalancedTree(Node root){
  System.out.println(findMaxHeight(root));
  System.out.println(findMinHeight(root));
 if(Math.abs(findMaxHeight(root)-findMinHeight(root)) <= 1)
  return true;
 else
  return false;
}

public static void main(String args[]){
 
 Tree newTree = new Tree();
 int[] inputValue = new int[]{5,2,3,6,8,7,1,9};
  for(int i=0;i< inputValue.length;i++){
    newTree.insert(inputValue[i]);
  }
 
 System.out.println(isBalancedTree(newTree.root));
}

}


Monday, August 5, 2013

Making Larger number from Given Array of Integer.

Ques: You have given an array of integers, using that form largest Number Possible.
Algorithm:

Source Code:(In java)
package com.array.exercise;

import java.util.Arrays;
import java.util.Comparator;

class MyCompare implements Comparator{

 public int compare(Integer i1,Integer i2){  
  String x =Integer.toString(i1);
  String y =Integer.toString(i2);
  
  String xy= x.concat(y);
  String yx= y.concat(x);
  return Integer.valueOf(xy)-Integer.valueOf(yx) > 0 ? 0:1;
 
 }
 
}
public class MakeBiggerNumber { 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 
 
   Integer[] input = new Integer[]{54,546,548,60,77};
 
    MyCompare mc = new MyCompare();
    Arrays.sort(input,mc);
 
     for (int i = 0; i < input.length; i++) {
  System.out.print(input[i]);
 }
 }

}