seeker7
BAN USEREvery Ant can move in 2 possible directions , backward and forward ( B or F ) , so the whole sample space consists of 8 choices, out of which 2 , namely BBB and FFF are no collision choices. so probability of not colliding is 2 /8 = 1/4.
F F B
F F F
F B B
F B F
B F B
B F F
B B B
B B F
node* LCA(node* nd1, node* nd2){
node* cur1 = nd1;
node* cur2 = nd2;
// calculate nd1 height
int nd1_height = 0;
while(cur1->parent!=NULL){
nd1_height++;
cur1 = cur1->parent;
}
// calculate nd2 height
int nd2_height = 0;
while(cur2->parent!=NULL){
nd2_height++;
cur2 = cur2->parent;
}
int diff = nd1_height - nd2_height;
cur1 = nd1;
cur2 = nd2;
if(diff>0){
while(diff--){
cur1 = cur1->parent;
}
} else{
diff = -diff;
while(diff--){
cur2 = cur2->parent;
}
}
while(1){
if(cur1==NULL || cur2==NULL) break;
if(cur1 == cur2)
return cur1;
cur1 = cur1->parent;
cur2 = cur2->parent;
}
return NULL;
}
hw abt this:
- seeker7 July 30, 2010rotate the given array n-1 times
then reverse the intial array and again rotate it n-1 times!