coder14
BAN USERAlgo:-
1. if number is greater than 5, check if odd. If odd, subtract 5.
2. if even, divide by 2. until it becomes an odd.
3. check in n=5.
The number will finally reduce to 5.
code:
public static boolean divisibleByFive(int n){
if(n==0)
return true;
if(n<0)
return divisibleByFive(n*(-1));
while(n>=5){
//if even divide by 2, until it becomes odd
while((n&1)==0)
n=n>>1;
//now check if equal to 5.
if(n==5)
return true;
else
n=n-5;
}
return false;
}
1. Imagine a list containing root of trees. For example, if we find an element (1,2) and next (3,4). We store them as two trees with roots 1 & 3.
2. So, as soon as we traverse through a list, we check the parent and child present in each treelist.
3. If parent and child both not found in any tree, append the tree in the list with parent as root.
3. If parent found and child not found in any tree, append the child to left or right of parent by checking.
4. If parent not found and child found in tree (child will be obviously in the root of the tree in the list, if not, then child already has parent -- not possible throw exception, not implemented in the code below),
5. If parent and child found, make child as left or right child of parent by checking, remove the child from the list of trees.
public static Node createTree(int[][] nodes){
//newList will hold non-linked trees. For example,
//if first element is (1,2) and next is (3,4)
//separate tree for (1,2) and (3,4) are created and root is stored in newList as an array of
//Tree.
ArrayList<Node> newList=new ArrayList<Node>();
for(int i=0;i<nodes.length;i++){
Node parent = findNode(newList, nodes[i][0]);
Node child = findChild(newList,nodes[i][1]);
//parent has not been found till now
if(parent==null){
parent = new Node(nodes[i][0]);
//child not found till now, create a new tree with parent as root and store in
// newList
if(child==null){
child = new Node(nodes[i][1]);
parent.left=child;
}
//child found in the newList but not parent if child found it will be always in the
// root
else{
parent.left=child;
newList.remove(child);
}
newList.add(parent);
}
else{
//parent exists -- two cases
//case 1: parent already has a left child
//case 2: parent is already a child of some Node
if(child==null){
child = new Node(nodes[i][1]);
if(parent.left==null)
parent.left=child;
else
parent.right=child;
}
//both parent and child exists
//child will be on top in newList. So, add it to parent and remove child from
// newlist
else{
if(parent.left==null)
parent.left=child;
else
parent.right=child;
newList.remove(child);
}
}
}
return newList.get(0);
}
public static Node findNode(ArrayList<Node> list, int element){
if(list==null)
return null;
for(int i=0;i<list.size();i++){
Node node = list.get(i);
Node found = find(node, element);
if(found!=null)
return found;
}
return null;
}
public static Node findChild(ArrayList<Node> list, int element){
if(list==null)
return null;
for(int i=0;i<list.size();i++){
Node node = list.get(i);
if(node.data == element)
return node;
}
return null;
}
public static Node find(Node root, int element){
if(root==null)
return null;
if(root.data==element)
return root;
Node node = find(root.left,element);
if(node!=null)
return node;
node = find(root.right,element);
if(node!=null)
return node;
return null;
}
- coder14 December 30, 2014