#include<stdio.h> int main() { int c; long int n,a; long int i,j; scanf("%ld",&n); for(a=1;a<=n;a++) { scanf("%ld %ld",&i,&j); c=0; while(1) { if(i>1&&i>j) { if(i%2==0) i=i/2; else i=(i-1)/2; c++; } if(i==j) break; else if(j>1) { if(j%2==0) j=j/2; else j=(j-1)/2; c++; } if(i==j) break; } printf("%d\n",c);
} return 0; }
you output for 1 32 8 is 8 but it should be 2
you are getting wrong answer because j should be divided by 2 only if it is greater than i , but you are just checking j>1 in that case j is getting divided whenever it is greater than 1, which is not correct.
change else if(j>1) to else if(j>1 && j>i)
You forgot to check , whether the node is left or right by finding the even and odd position nodes.
true n thanks