pratap.raavi
BAN USERIt can be used at 3 different levels
Class level : if I do not want anyone to create sub class of my class
Ex: java.lang.String
Method level : if I do not want anyone to override that particular method in subclass
Variable level : If i do not want reset a value which is set once. It allows to set once.
In java we can use java.lang.Number which is parent class of Integer and Float
{{
Number array[]={1,1.2f,4.4f,4};
}}
Java 1.5 or higher version will support auto boxing so no need to do any thing
If i want to store them in sorted order i would use TreeSet<Number, myComparator>()
myComparator would implement Comparator<Number>
If i want to sort unsorted list, i would go for Collections.sort(list, myCompartator);
final cannot be used for
1. methods inside interface (by default all the methods are public abstract)
2. abstract class
3. abstract methods
Reason : abstract means the class/method need to be extended/overridden. Which is opposite of final. Hence compiler will throw exception
typo error. pushed leftChild twice :)
stack1.push(current.leftChild);
stack1.push(current.rightChild);
This the best i could think of iterative approach
static int binaryLevelnonRec(Tree head){
if(head==null)
return 0;
LinkedList<Tree> stack1 = new LinkedList<Tree>();
head.depth =1;
stack1.push(head);
int maxCompleteDepth = -1;
while(!stack1.isEmpty()){
Tree current = stack1.pop();
int currDepth = current.depth;
if(maxCompleteDepth !=-1 && maxCompleteDepth<currDepth)
continue;
if(current.leftChild!=null && current.rightChild!=null){
current.leftChild.depth = currDepth+1;
current.rightChild.depth = currDepth+1;
stack1.push(current.leftChild);
stack1.push(current.leftChild);
} else
maxCompleteDepth = currDepth;
}
return maxCompleteDepth;
}
java recursive code
static int binaryLevelRec(Tree head){
if(head==null)
return 0;
else
return 1+ Math.min(binaryLevelRec(head.leftChild), binaryLevelRec(head.rightChild));
}
I guess this should do
void union(int[] a, int[]b){
int smallArray[] = null;
int largeArray[] = null;
if(a.length<b.length){
smallArray = a;
largeArray = b;
}else{
smallArray = a;
largeArray = b;
}
HashSet<Integer> set = new HashSet<Integer>();
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.ensureCapacity(largeArray.length+smallArray.length);
for(int i:smallArray)
set.add(i);
for(int i :largeArray)
if(!set.contains(i))
arr.add(i);
for(int i: set)
System.out.print(i + " ");
for(int i:arr)
System.out.print(i+ " ");
}
i think the output would be intersection of 2 arrays
- pratap.raavi July 26, 2012//if the array is sorted
//worst case complexity O(n)
static void sortedArrayApproch(int sortedArray[]){
if(null==sortedArray ){
System.out.println("not threr");
System.exit(0);
}
int i=0, j = sortedArray.length-1, leftsum=0, rightsum =0;
int loopcount =0;
System.out.println(sortedArray.length);
while(i<j){
loopcount++;
if(leftsum<rightsum)
leftsum+=sortedArray[i++];
else
rightsum+=sortedArray[j--];
if(((j-i)*sortedArray[j])<Math.abs(rightsum-leftsum)){
System.out.println(loopcount);
System.out.println("cannot be fount");
System.exit(0);
}
}
System.out.println(loopcount);
if(leftsum == rightsum)
System.out.println(i);
else
System.out.println("not there");
}
//if we does not know whether array is sorted
//O(n)
static void unSortedArrayApproch(int a[]){
if(null==a ){
System.out.println("not threr");
System.exit(0);
}
int i=0, j = a.length-1, leftsum=0, rightsum =0;
while(i<j){
if(leftsum<rightsum)
leftsum+=a[i++];
else
rightsum+=a[j--];
}
if(leftsum == rightsum)
System.out.println(i);
else
System.out.println("not there");
}
As per my understanding, function will take address of nth node as input.
- pratap.raavi July 30, 2012If it is doubly linked list,
(n-1)->rlink=(n+1)
(n+1)->llink=(n-1)
free(n)
If its circular linked list,
1. Iterate and find (n-1)th node.
2. (n-1)->link = (n+1)
3. free(n)