Given an unweighted and undirected graph consisting of N nodes and two integers a and b. The edge between any two nodes exists only if the bit difference between them is 2, the task is to find the length of the shortest path between the nodes a and b. If a path does not exist between the nodes a and b, then print -1.
Examples:
Input: N = 15, a = 15, b = 3
Output: 1
Explanation: a = 15 = (1111)2 and b = 3 = (0011)2. The bit difference between 15 and 3 is 2. Therefore, there is a direct edge between 15 and 3. Hence, length of the shortest path is 1.Input: N = 15, a = 15, b = 2
Output: -1
Explanation: a = 15 = (1111)2 and b= 2 = (0010)2. The bit difference between 15 and 2 is 3. As the bit difference can only be 2, it is impossible to reach 15
from 2.
Naive Approach: The simplest approach to solve this problem is to first construct the graph using the given conditions, then find the shortest path between the nodes using a and b using bfs by considering a as the source node of the graph.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach:The problem can be solved by observing that the sum of bit differences between any two nodes must be a factor 2 and their shortest distance must be half of that sum. Follow the steps given below to understand the approach:
- Count of set bits in Bitwise XOR of a and b gives the count of bit difference between the nodes a and b.
- If the count of set bits in Bitwise XOR of a and b is a multiple of 2, then a and b are connected.
- If the count of set bits is 2, that means they are 1 unit apart from each other.If the count of set bits in xor of a and b is 4 that means node a and b are 2 units apart. Therefore, if the bit difference is x then the shortest path would be x/2.
- If the bit difference is odd then they are not connected, therefore, print -1.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count set bits // in a number int countbitdiff( int xo) { // Stores count of // set bits in xo int count = 0; // Iterate over each // bits of xo while (xo) { // If current bit of xo // is 1 if (xo % 2 == 1) { // Update count count++; } // Update xo xo = xo / 2; } return count; } // Function to find length of shortest // path between the nodes a and b void shortestPath( int n, int a, int b) { // Stores XOR of a and b int xorVal = a ^ b; // Stores the count of // set bits in xorVal int cnt = countbitdiff(xorVal); // If cnt is an even number if (cnt % 2 == 0) cout << cnt / 2 << endl; else cout << "-1" << endl; } // Driver Code int main() { // Given N int n = 15; // Given a and b int a = 15, b = 3; // Function call shortestPath(n, a, b); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to count set bits // in a number static int countbitdiff( int xo) { // Stores count of // set bits in xo int count = 0 ; // Iterate over each // bits of xo while (xo != 0 ) { // If current bit of xo // is 1 if (xo % 2 == 1 ) { // Update count count++; } // Update xo xo = xo / 2 ; } return count; } // Function to find length of shortest // path between the nodes a and b static void shortestPath( int n, int a, int b) { // Stores XOR of a and b int xorVal = a ^ b; // Stores the count of // set bits in xorVal int cnt = countbitdiff(xorVal); // If cnt is an even number if (cnt % 2 == 0 ) System.out.print(cnt / 2 ); else System.out.print( "-1" ); } // Driver Code public static void main(String[] args) { // Given N int n = 15 ; // Given a and b int a = 15 , b = 3 ; // Function call shortestPath(n, a, b); } } // This code is contributed by susmitakundugoaldanga |
Python3
# Python3 program for the above approach # Function to count set bits # in a number def countbitdiff(xo): # Stores count of # set bits in xo count = 0 # Iterate over each # bits of xo while (xo): # If current bit of xo # is 1 if (xo % 2 = = 1 ): # Update count count + = 1 # Update xo xo = xo / / 2 return count # Function to find length of shortest # path between the nodes a and b def shortestPath(n, a, b): # Stores XOR of a and b xorVal = a ^ b # Stores the count of # set bits in xorVal cnt = countbitdiff(xorVal) # If cnt is an even number if (cnt % 2 = = 0 ): print (cnt / / 2 ) else : print ( "-1" ) # Driver Code if __name__ = = '__main__' : # Given N n = 15 # Given a and b a,b = 15 , 3 # Function call shortestPath(n, a, b) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; class GFG { // Function to count set bits // in a number static int countbitdiff( int xo) { // Stores count of // set bits in xo int count = 0; // Iterate over each // bits of xo while (xo != 0) { // If current bit of xo // is 1 if (xo % 2 == 1) { // Update count count++; } // Update xo xo = xo / 2; } return count; } // Function to find length of shortest // path between the nodes a and b static void shortestPath( int n, int a, int b) { // Stores XOR of a and b int xorVal = a ^ b; // Stores the count of // set bits in xorVal int cnt = countbitdiff(xorVal); // If cnt is an even number if (cnt % 2 == 0) Console.Write(cnt / 2); else Console.Write( "-1" ); } // Driver code public static void Main (String[] args) { // Given N int n = 15; // Given a and b int a = 15, b = 3; // Function call shortestPath(n, a, b); } } // This code is contributed by code_hunt. |
Javascript
<script> // Javascript program for the above approach // Function to count set bits // in a number function countbitdiff(xo) { // Stores count of // set bits in xo let count = 0; // Iterate over each // bits of xo while (xo) { // If current bit of xo // is 1 if (xo % 2 == 1) { // Update count count++; } // Update xo xo = Math.floor(xo / 2); } return count; } // Function to find length of shortest // path between the nodes a and b function shortestPath(n, a, b) { // Stores XOR of a and b let xorVal = a ^ b; // Stores the count of // set bits in xorVal let cnt = countbitdiff(xorVal); // If cnt is an even number if (cnt % 2 == 0) document.write(cnt / 2 + "<br>" ); else document.write( "-1" + "<br>" ); } // Driver Code // Given N let n = 15; // Given a and b let a = 15, b = 3; // Function call shortestPath(n, a, b); // This code is contributed by gfgking. </script> |
1
Time Complexity: O(log2(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!