Shortest Path between Cities | Practice | GeeksforGeeks

Geek lives in a special city where houses are arranged in a hierarchial manner. Starting from house number 1, each house leads to two more houses.  
1 leads to 2 and 3. 
2 leads to 4 and 5. 
3 leads to 6 and 7. and so on. 
Given the house numbers on two houses x and y, find the length of the shortest path between them. 

Example 1:
Input:
x = 2, y = 6
Output: 3
Explanation:
              1
          /      \
        /          \
       2             3
     /   \         /   \
    4     5       6     7         
   / \   / \     / \   / \
  8  9  10 11   12 13 14 15

The length of the shortest path between 2 
and 6 is 3. ie 2-> 1-> 3-> 6.

Example 2:
Input:
x = 8, y = 10
Output: 4
Explanation: 8-> 4-> 2-> 5-> 10
The length of the shortest path between 8 
and 10 is 4. 

Your Task:
You don't need to read input or print anything. Complete the function shortestPath() which takes integers x and y as input parameters and returns the length of the shortest path from x to y.

Expected Time Complexity: O(log(max(x,y)))
Expected Auxiliary Space: O(1)

Constraints: 
1 <= x,y <= 109
+1
harendraseervi1234567893 months ago
   int shortestPath( int x, int y){        int lx=0;       int ly=0;       while(x!=y){           if(x>y){               x=x/2;               lx++;           }           else if(y>x){               y=y/2;               ly++;           }       }       return lx+ly;   }
+1
keshavkumarshivanshu33 months ago
Simple logic-Find lowest common ancestor i.e., find nearest root to both the number.
int shortestPath( int x, int y){ 
        if(x==y) return 0;
        if(x>y){
            return 1+shortestPath(x/2,y);
        }else{
            return 1+shortestPath(x,y/2);
        }
    }
0
kake13375 months ago
class Solution{   public:   int shortestPath( int x, int y){    int r=0;   while(x!=y)   {       if(x>y)       x/=2;       else       y/=2;       r++;   }   return r;   }};
0
abhishekpanwar6976 months ago
int shortestPath( int x, int y){        int count=0;       while(x!=y)       {           if(x>y)           x=x/2;           else           y=y/2;           count++;       }       return count;   }
0
Dhruv Mishra_18267 months ago
Dhruv Mishra_1826
exe time 0.0/2.2
 https://uploads.disquscdn.c...
0
Meikandanathan Pandian9 months ago
Meikandanathan Pandian
Accepted solution in java -> https://onlinegdb.com/sQAz2...
+1
shady419 months ago
shady41
The parent of any element will be floor(n/2)So we just keep finding parent for the larger until they become equal.
     int parent(int n){        return n/2;    }    int shortestPath( int x, int y){         if(x==y){            return 0;        }        int ans=0;        while(x!=y){            ans+=1;            if(x>y){                x=parent(x);            }            else{                y=parent(y);            }        }        return ans;    }
+1
Nihal Chaturvedi11 months ago
Nihal Chaturvedi
The Question says for any number say x it leads to 2 more houses that are 2*x and (2*x)+1 . If you can remember this is exactly the case with heap.So we know in heap that for any element x parent is : (x-1)//2Here since we are starting from 1 parent will be x//2Now what is the intution is  we try to go to the parent of larger of (x,y) untill we get x ==y or any one of them gets 1 .Now after the loop if they are not equal means one of them reached the 1 ( root ) so the other one should also reach the root . If they are equal then nothing more to do .
Example say x= 8 and y =14We are running loop ---->x=8,y=14:     y is larger so y gets y//2 i.e. 7  and count becomes 1x=8,y=7:       x is larger so x gets x//2  i.e.  4 and count becomes 2x=4,y=7:       y is larger so y gets y//2 i.e. 3 and count becomes 3x=4,y=3:       x is larger so x gets x//2 i.e. 2 and count becomes 4x=2,y=3:       y is larger so y gets y//2 i.e. 1 and count becomes 5 Now x=2,y=1 so end loopNow x and y are not equal so :             y=1 then make x 1:                    x   =   x//2 -->  x=1  and count becomes 6return count
 https://uploads.disquscdn.c...
0
Rithik Raj1 year ago
Rithik Raj
class Solution{   public:    int shortestPath( int x, int y){         int a = max(x,y);        int b = min(x,y);int cx = 0,cy = 0;        while(a != b){            if(a>b){                a = a/2;                cx++;            }else if(b>a){                b = b/2;                cy++;            }        }        int ans = cy+cx;        return ans;    }};
0
Rahul Kumar Vairagade1 year ago
Rahul Kumar Vairagade
Execution Time: 0.01
int shortestPath( int x, int y){         int spath = 0;        while(x!=y){            if(x>y)                x = x/2;            else                y = y/2;            spath++;        }        return spath;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.