Minimum steps to make X equal to Y by performing increment or bitwise OR operation
Given two integers X and Y, the task is to find the minimum number of operations required to make X equal to Y by performing the given operations such that:
- X = X + 1
- Y = Y + 1
- X = X | Y
Examples:
Input: X = 4, Y = 5
Output: 1
Explanation: Applying 1st operation one time X = X + 1 => 4 + 1 => 5 (Thus, both numbers become equal).
Input: X = 2, Y = 5
Output: 2
Explanation: Apply second operation on Y and then 3rd operation (bitwise OR with Y).
So first Y will become 6 and then 2|6 = 6 in second step.
Approach: The problem can be solved using bitwise techniques as per the following observation:
- Operation 3 should be applied only once, as (X | Y) is always greater than or equal to max(X, Y)
- After operation 3, because of the reason mentioned the next operation (if needed) will always be incrementing Y by 1 to have minimum number of steps.
- If operation 3 is not used at any time, answer = Y – X.
Now to generalize the above idea in terms of formula and cost for each step :-
let’s say the initial value is given = (X, Y) -> and we would be calculating cost at each step as well.
- Then Cost1 => 0, (initial cost)
- The increased value = (newX, newY) (assume newX and newY as new values if they are increased some number of times)
Then difference of increased value with original(initial) value) Cost2 = [(newX – Y) + (newY – Y)]
- Applying operation 3(only once as discussed above) on previous term X and Y will be [(newX | newY), newY]
So cost for this will be Cost3 = Cost2 + 1\
- After applying operation 2 on previous terms to make them equal, they will be [(newX | newY), (newX | newY)]
Then Cost4 = Cost3 + (newX | newY – newY)(i.e, this would turn out to be cost for making newY equal to newX term)
- Thus, overall cost after reducing terms would be = Cost2 + Cost3 + Cost4 = newX + (newX | newY) + [1 – X – Y].
- Assuming the last term to be constant our final equation would turn out to be => newX + (newX | newY)
[Reduced Term]
To minimize the overall cost, we have to minimize the reduced term. For doing so, iterate over all possible values of newX and greedily find the value of newY. To construct newY, iterate from MSB(highest bit) to LSB(lowest bit).
Follow the steps mentioned below to minimize the value of reduced term mentioned in the observation:
- If the ith bit is set in newX, then:
- If the bit is not set in Y, set bit in newY and break;
- Else, set the bit in newY as well.
- if the ith bit is not set in newX, then-
- newY for that bit would be equal to Y for that bit.
- In the end, compute the minimum for each iteration, and return the final minimum answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int x, int y)
{
int answer = 0;
answer = y - x;
for ( int newX = x; newX <= y; newX++) {
int newY = 0;
for ( int i = 20; i >= 0; i--) {
if ((newX & (1 << i))) {
if (!(y & (1 << i))) {
newY += (1 << i);
break ;
}
else {
newY += (1 << i);
}
}
else {
if (y & (1 << i)) {
newY += (1 << i);
}
else {
newY += 0;
}
}
}
answer = min(answer,
newX + (newX | newY)
+ (1 - x - y));
}
return answer;
}
int main()
{
int X = 2, Y = 5;
cout << solve(X, Y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int solve( int x, int y)
{
int answer = 0 ;
answer = y - x;
for ( int newX = x; newX <= y; newX++) {
int newY = 0 ;
for ( int i = 20 ; i >= 0 ; i--) {
if ((newX & ( 1 << i)) != 0 ) {
if ((y & ( 1 << i)) == 0 ) {
newY += ( 1 << i);
break ;
}
else {
newY += ( 1 << i);
}
}
else {
if ((y & ( 1 << i)) != 0 ) {
newY += ( 1 << i);
}
else {
newY += 0 ;
}
}
}
answer = Math.min(answer, newX + (newX | newY)
+ ( 1 - x - y));
}
return answer;
}
public static void main (String[] args)
{
int X = 2 , Y = 5 ;
System.out.print(solve(X, Y));
}
}
|
Python3
def solve(x, y):
answer = 0
answer = y - x;
for newX in range (x,y + 1 ):
newY = 0
for i in range ( 20 , - 1 , - 1 ):
if ((newX & ( 1 << i))):
if (~(y & ( 1 << i))):
newY + = ( 1 << i)
break
else :
newY + = ( 1 << i)
else :
if (y & ( 1 << i)):
newY + = ( 1 << i)
else :
newY + = 0
answer = min (answer,newX + (newX | newY) + ( 1 - x - y))
return answer
X,Y = 2 , 5
print (solve(X, Y))
|
C#
using System;
class GFG {
static int solve( int x, int y)
{
int answer = 0;
answer = y - x;
for ( int newX = x; newX <= y; newX++) {
int newY = 0;
for ( int i = 20; i >= 0; i--) {
if ((newX & (1 << i)) != 0) {
if ((y & (1 << i)) == 0) {
newY += (1 << i);
break ;
}
else {
newY += (1 << i);
}
}
else {
if ((y & (1 << i)) != 0) {
newY += (1 << i);
}
else {
newY += 0;
}
}
}
answer = Math.Min(answer, newX + (newX | newY)
+ (1 - x - y));
}
return answer;
}
public static void Main()
{
int X = 2, Y = 5;
Console.Write(solve(X, Y));
}
}
|
Javascript
<script>
function solve(x, y)
{
let answer = 0;
answer = y - x;
for (let newX = x; newX <= y; newX++) {
let newY = 0;
for (let i = 20; i >= 0; i--) {
if ((newX & (1 << i))) {
if (!(y & (1 << i))) {
newY += (1 << i);
break ;
}
else {
newY += (1 << i);
}
}
else {
if (y & (1 << i)) {
newY += (1 << i);
}
else {
newY += 0;
}
}
}
answer = Math.min(answer,
newX + (newX | newY)
+ (1 - x - y));
}
return answer;
}
let X = 2, Y = 5;
document.write(solve(X, Y));
</script>
|
Time Complexity: O(Y * logY)
Auxiliary Space: O(1)
Last Updated :
01 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...