Gourab
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
pseudo-code:
int traverse(node n) {
if(n is either of the given nodes) {
return 1;
}
if (n is leaf node) {
return 0;
}
else {
value = 0;
foreach (node m : children of n) {
value += traverse(m);
if (value>1) {
node n is the least parent;
exit; // r return 0 if you cannot exit
}
}
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
yes u r right.... i missed 2 in the array.
- Gourab January 23, 2013my solution:
int findMaxIndex (arr, start, k) {
maxI = start;
for(i=start+1; i<start+k; i++) {
if (arr[i] > arr[maxI]) {
maxI = i;
}
}
return maxI;
}
void printMaxInSubArray(arr, k) {
maxI = findMaxIndex(arr, 0, k);
print arr[maxI];
for (i=k; i<arr.size; i++){
if (arr[i] > arr[maxI] ) {
maxI = i;
}
print arr[maxI];
}
}