Daily Archives: December 16, 2014

Find the median in an array

Problem: If a random array is [2, 20, 40, 60, 50, 65, 25, 10, 30, 35,  52, 63, 67, 3], find the median of the array.

1. For each new Element in array, if it is less than the root of max heap, put it in the max heap; or put it in the min heap.
2. Adjust 2 heaps, in order that number of element in 2 heaps differ less than 1.
If element numbers in 2 heaps differ more than 1, put the root of the heap which has more element in the heap which has less element. Adjust 2 heaps.

Put all elements into 2 heaps.
If number of elements are the same, the median is (root_of_max_heap + root_of_min_heap) / 2.
If number of elements are not the same, the median is the root of heap which has more element.

Division with cycle remainder

Divide number and return result in form of a string. e.g 100/3 result should be 33.(3) Here 3 is in brackets because it gets repeated continuously and 5/10 should be 0.5.

The essence is to use hashmap to restore the remainder. Once a certain remainder was obtained for 2nd time, we found the recycles.

  1. public class DivisionWithCycleRemainder {
  2.     public static void main(String[] args){
  3.         String result = divisionToString(45,17);
  4.         System.out.println(result);
  5.     }
  6.     public static String divisionToString(int num1, int num2) {
  7.         int beforeD = num1 / num2;    //the result before dot
  8.         int afterD = num1 % num2;    //the result after dot
  9.         if(afterD==0){
  10.             return String.valueOf(beforeD);
  11.         }
  12.         HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
  13.         StringBuffer result = new StringBuffer(beforeD + “.”);
  14.         int remainder = afterD * 10;
  15.         while(!hm.containsKey(remainder)){
  16.             hm.put(remainder, result.length());    //remainder never exist, insert into hm
  17.             int curr_value = remainder / num2;    //result of current position
  18.             result.append(curr_value);
  19.             remainder = (remainder % num2) * 10;
  20.         }
  21.         result.insert(hm.get(remainder).intValue(), ‘(‘);
  22.         result.append(‘)’);
  23.         return result.toString();
  24.     }
  25. }

Construct BST by in-order traversal

Given a sequence of an in-order traversal 4,3,1,2,7,5,8, construct the BST.

The basic idea:
The idea is that a sub-tree can be buildt only if the next value is within the  min and max value of its current node..

On thing which needs to be mentioned, is that there is not unique BST for a in-order traversal sequence.

public class ConstructBSTPreOrder {

public static void main(String[] args) {
int[] array = {4,3,1,2,7,5,8};
MyInteger pos = new MyInteger(0);
Tree tree = constructBSTByPreOrder(array, pos, Integer.MIN_VALUE, Integer.MAX_VALUE);
System.out.println(tree);
}

public static Tree constructBSTByPreOrder(int[] array, MyInteger pos, int min, int max){
if(array==null||pos.i>=array.length){
return null;
}
if(array[pos.i]<min||array[pos.i]>max){
return null;
}
Tree tree = new Tree(array[pos.i]);
pos.i++;
tree.left = constructBSTByPreOrder(array, pos, min, tree.value);
tree.right = constructBSTByPreOrder(array, pos, tree.value, max);
return tree;
}
}