Minimize steps to make two integers equal by incrementing them or doing bitwise OR of them
Given two positive integers A and B. The task is to make them equal using minimum operations such that:
- A = A + 1 (increase a by 1).
- B = B + 1 (increase b by 1).
- A = A | B (replace A with the bitwise OR of A and B).
Examples:
Input: A = 5, B = 9
Output: 4
Explanation: It is better to use first operation i.e increase A by 1 four times,
Input: A = 2, B = 5
Output: 2
Explanation: It is better to apply second operation then third operation,
Greedy Approach: The problem can be solved using Greedy technique with the help of Bit manipulation.
Intuition:
- Try to increase A or B by 1, the steps will be the maximum steps possible.
- Now to reduce these steps,
- We need to find an intermediate number X such that X OR B = B, because only then we can jump more than 1 number in a single step.
- Once we have found possible values for X, we can check that which value among them is reachable for A in least steps.
- Those least steps + 1 step (for doing bitwise OR of X with B) will be one of the lesser number of steps for A to reach B.
- Another way to reduce these steps:
- Consider the case when instead of making X OR B = B, we find possible values of Y such that A OR Y = Y, as B can also be moved as per given problem.
- So we can find least step needed to move B to Y and then add 1 more step to do bitwise OR of A with B.
- Now try to find the minimum among the both possible lesser steps as the required number of steps to change A to B.
Illustration:
Suppose A = 2, B = 5
Case 1: Possible value of X such that (X OR B = B) => [0, 1, 4, 5]
Now the steps required to convert A to B if we convert A to each possible value of X first, are:
Convert A to 0 => not possible as we cannot decrement A
Convert A to 1 => not possible as we cannot decrement A
Convert A to 4 => 2 increment operation, and then 1 operation for 4 OR 5 to make A as 5. Hence total operation = 3
Convert A to 5 => 3 increment operation to make A as 5. Hence total operation = 3
Case 2: Possible value of Y such that (A OR Y = Y) => [2, 6, 7, …]
Now the steps required to convert A to B if we convert B to each possible value of Y first, are:
Convert B to 2 => not possible as we cannot decrement B
Convert B to 6 => 1 increment operation, and then 1 operation for 2 OR 6 to make A as 6. Hence total operation = 2
Convert B to 7 => 2 increment operation, and then 1 operation for 2 OR 7 to make A as 7. Hence total operation = 3
Similarly for any conversion of B to value greater than 7 will take more steps.Therefore the least steps required to convert A to B using given operations = min(3, 2) = 2 steps.
Follow the steps mentioned below to implement the approach:
- Iterate from i = A to B and check if (i | B) is the same as B and the steps required for that.
- Find the minimum steps (say x) required to make A and B equal in this way.
- Now iterate for j = B to B+x:
- Check if that j satisfies case 2 as mentioned above.
- Update the minimum steps required to make A and B equal.
- Return the minimum number of steps.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find min steps // to convert A to B int AToB( int a, int b) { int ans = INT_MAX; // If a is greater than b, swap them if (a > b) { int temp = b; b = a; a = temp; } // Check for every i from 0 to b for ( int i = a; i <= b; i++) { // If i or b equals to b then // update the answer if ((i | b) == b) { int j = abs (a - i); if (i != b) j++; ans = min(ans, j); } } for ( int i = b + 1; i <= b + ans; i++) { if ((i | a) == i) { ans = min(ans, i - b + 1); } } return ans; } // Driver Code int main() { int A = 2; int B = 5; cout << AToB(A, B); return 0; } // This code is contributed by Rohit Pradhan |
Java
// Java code to implement the approach import java.io.*; import java.lang.*; class GFG { // Function to find min steps // to convert A to B public static int AToB( int a, int b) { int ans = Integer.MAX_VALUE; // If a is greater than b, swap them if (a > b) { int temp = b; b = a; a = temp; } // Check for every i from 0 to b for ( int i = a; i <= b; i++) { // If i or b equals to b then // update the answer if ((i | b) == b) { int j = Math.abs(a - i); if (i != b) j++; ans = Math.min(ans, j); } } for ( int i = b + 1 ; i <= b + ans; i++) { if ((i | a) == i) { ans = Math.min(ans, i - b + 1 ); } } return ans; } // Driver Code public static void main(String[] args) { int A = 2 ; int B = 5 ; System.out.println(AToB(A, B)); } } |
Python3
# Python3 code to implement the approach INT_MAX = 2147483647 # Function to find min steps # to convert A to B def AToB(a, b): ans = INT_MAX # If a is greater than b, swap them if (a > b): temp = b b = a a = temp # Check for every i from 0 to b for i in range (a, b + 1 ): # If i or b equals to b then # update the answer if ((i | b) = = b): j = abs (a - i) if (i ! = b): j + = 1 ans = min (ans, j) for i in range (b + 1 , b + ans + 1 ): if ((i | a) = = i): ans = min (ans, i - b + 1 ) return ans # Driver Code if __name__ = = "__main__" : A = 2 B = 5 print (AToB(A, B)) # This code is contributed by rakeshsahni |
C#
// C# code to implement the above approach using System; class GFG { // Function to find min steps // to convert A to B public static int AToB( int a, int b) { int ans = Int32.MaxValue; // If a is greater than b, swap them if (a > b) { int temp = b; b = a; a = temp; } // Check for every i from 0 to b for ( int i = a; i <= b; i++) { // If i or b equals to b then // update the answer if ((i | b) == b) { int j = Math.Abs(a - i); if (i != b) j++; ans = Math.Min(ans, j); } } for ( int i = b + 1; i <= b + ans; i++) { if ((i | a) == i) { ans = Math.Min(ans, i - b + 1); } } return ans; } // Driver code public static void Main() { int A = 2; int B = 5; Console.Write(AToB(A, B)); } } // This code is contributed by sanjoy_62. |
Javascript
<script> // JavaScript code to implement the approach // Function to find min steps // to convert A to B function AToB(a, b) { var ans = Number.MAX_SAFE_INTEGER; // If a is greater than b, swap them if (a > b) { var temp = b; b = a; a = temp; } // Check for every i from 0 to b for ( var i = a; i <= b; i++) { // If i or b equals to b then // update the answer if ((i | b) == b) { var j = Math.abs(a - i); if (i != b) j++; ans = Math.min(ans, j); } } for ( var i = b + 1; i <= b + ans; i++) { if ((i | a) == i) { ans = Math.min(ans, i - b + 1); } } return ans; } // Driver Code var A = 2; var B = 5; document.write(AToB(A, B)); // This code is contributed by phasing17 </script> |
2
Time Complexity: O(B * log B)
Auxiliary Space: O(1)
Please Login to comment...