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++
#include <bits/stdc++.h>
using namespace std;
int AToB( int a, int b)
{
int ans = INT_MAX;
if (a > b) {
int temp = b;
b = a;
a = temp;
}
for ( int i = a; i <= b; i++) {
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;
}
int main()
{
int A = 2;
int B = 5;
cout << AToB(A, B);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
class GFG {
public static int AToB( int a, int b)
{
int ans = Integer.MAX_VALUE;
if (a > b) {
int temp = b;
b = a;
a = temp;
}
for ( int i = a; i <= b; i++) {
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;
}
public static void main(String[] args)
{
int A = 2 ;
int B = 5 ;
System.out.println(AToB(A, B));
}
}
|
Python3
INT_MAX = 2147483647
def AToB(a, b):
ans = INT_MAX
if (a > b):
temp = b
b = a
a = temp
for i in range (a, b + 1 ):
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
if __name__ = = "__main__" :
A = 2
B = 5
print (AToB(A, B))
|
C#
using System;
class GFG {
public static int AToB( int a, int b)
{
int ans = Int32.MaxValue;
if (a > b) {
int temp = b;
b = a;
a = temp;
}
for ( int i = a; i <= b; i++) {
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;
}
public static void Main()
{
int A = 2;
int B = 5;
Console.Write(AToB(A, B));
}
}
|
Javascript
<script>
function AToB(a, b)
{
var ans = Number.MAX_SAFE_INTEGER;
if (a > b) {
var temp = b;
b = a;
a = temp;
}
for ( var i = a; i <= b; i++) {
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;
}
var A = 2;
var B = 5;
document.write(AToB(A, B));
</script>
|
Time Complexity: O(B * log B)
Auxiliary Space: O(1)
Last Updated :
21 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...