Count of operations to make numbers equal by adding power of 2
Last Updated :
28 Jul, 2022
Given three positive integers X, Y, and Z and an integer N, the task is to make them equal by adding 2i-1 for the ith operation to any one of these 3 numbers. Find the number of moves required to make these 3 numbers equal where the total number of operations performed is N. If they cannot be made equal print -1.
Examples:
Input: X = 1, Y = 6, Z = 7, N = 30
Output: 3
Explanation: Here in first operation we add 21-1 = 20 = 1 to Y so that it become equal to Z and in second move we add 22-1 = 2 to X and value of X become 3 and then in third move we add 23-1=4 to X such that its value become 7. Hence these three numbers are equal in 3 moves.
Input: X = 4, Y = 5, Z = 4, N = 20
Output: -1
Approach: The problem can be solved based on the following observation:
Observations:
- Here 2i-1 has one set bit i.e. 01,10,100,1000 and so on .
- We have to convert these 3 numbers X, Y, and Z into binary numbers and make their least significant bit equal by adding 2i-1 to any one of these three numbers and doing same thing to each bit of the number until the most significant bit becomes equal also count the number of moves simultaneously.
- After performing operation if all bits of these three numbers get equal then print the number of moves else print -1.
Follow the steps mentioned below to implement the idea:
- Start iterating from i = 0 to N-1:
- Check if adding 2i to any number makes the ith least significant bit same for all of them.
- If not then return -1.
- Otherwise, continue the process till all the numbers are the same or N steps are performed.
- If all the number becomes equal then return the steps required.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int chk( int n, int bit) { return ((n & bit) != 0) ? 1 : 0; }
int minMove( int X, int Y, int Z, int N)
{
int count = 0;
bool ans = true ;
for ( int i = 0; i <= N; i++) {
if (X == Y && Y == Z)
break ;
int bit = (1 << i);
int cnt = chk(X, bit) + chk(Y, bit) + chk(Z, bit);
if (cnt == 0 || cnt == 3) {
ans = false ;
break ;
}
else if (cnt == 1) {
if (chk(X, bit) == 1)
X += bit;
else if (chk(Y, bit) == 1)
Y += bit;
else
Z += bit;
}
else {
if (chk(X, bit) == 0)
X += bit;
else if (chk(Y, bit) == 0)
Y += bit;
else
Z += bit;
}
count++;
}
return (ans && X == Y && Y == Z) ? count : -1;
}
int main()
{
int X = 1, Y = 6, Z = 7, N = 30;
cout << minMove(X, Y, Z, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int chk( int n, int bit)
{
return ((n & bit) != 0 ) ? 1 : 0 ;
}
public static int minMove( int X, int Y,
int Z, int N)
{
int count = 0 ;
boolean ans = true ;
for ( int i = 0 ; i <= N; i++) {
if (X == Y && Y == Z)
break ;
int bit = ( 1 << i);
int cnt
= chk(X, bit) + chk(Y, bit) + chk(Z, bit);
if (cnt == 0 || cnt == 3 ) {
ans = false ;
break ;
}
else if (cnt == 1 ) {
if (chk(X, bit) == 1 )
X += bit;
else if (chk(Y, bit) == 1 )
Y += bit;
else
Z += bit;
}
else {
if (chk(X, bit) == 0 )
X += bit;
else if (chk(Y, bit) == 0 )
Y += bit;
else
Z += bit;
}
count++;
}
return (ans && X == Y && Y == Z) ? count : - 1 ;
}
public static void main(String[] args)
{
int X = 1 , Y = 6 , Z = 7 , N = 30 ;
System.out.println(minMove(X, Y, Z, N));
}
}
|
Python3
def chk(n, bit):
return 1 if (n & bit) ! = 0 else 0
def minMove(X, Y, Z, N):
count = 0
ans = True
for i in range (N + 1 ):
if X = = Y and Y = = Z:
break
bit = ( 1 << i)
cnt = chk(X, bit) + chk(Y, bit) + chk(Z, bit)
if cnt = = 0 or cnt = = 3 :
ans = False
break
elif cnt = = 1 :
if chk(X, bit) = = 1 :
X + = bit
elif chk(Y, bit) = = 1 :
Y + = bit
else :
Z + = bit
else :
if chk(X, bit) = = 0 :
X + = bit
elif chk(Y, bit) = = 0 :
Y + = bit
else :
Z + = bit
count + = 1
return count if ans and X = = Y and Y = = Z else - 1
if __name__ = = '__main__' :
X = 1
Y = 6
Z = 7
N = 30
print (minMove(X, Y, Z, N))
|
C#
using System;
public class GFG {
static int chk( int n, int bit)
{
return ((n & bit) != 0) ? 1 : 0;
}
public static int minMove( int X, int Y,
int Z, int N)
{
int count = 0;
bool ans = true ;
for ( int i = 0; i <= N; i++) {
if (X == Y && Y == Z)
break ;
int bit = (1 << i);
int cnt
= chk(X, bit) + chk(Y, bit) + chk(Z, bit);
if (cnt == 0 || cnt == 3) {
ans = false ;
break ;
}
else if (cnt == 1) {
if (chk(X, bit) == 1)
X += bit;
else if (chk(Y, bit) == 1)
Y += bit;
else
Z += bit;
}
else {
if (chk(X, bit) == 0)
X += bit;
else if (chk(Y, bit) == 0)
Y += bit;
else
Z += bit;
}
count++;
}
return (ans && X == Y && Y == Z) ? count : -1;
}
public static void Main(String[] args)
{
int X = 1, Y = 6, Z = 7, N = 30;
Console.WriteLine(minMove(X, Y, Z, N));
}
}
|
Javascript
<script>
function chk(n,bit) { return ((n & bit) != 0) ? 1 : 0; }
function minMove( X, Y, Z, N)
{
let count = 0;
let ans = true ;
for (let i = 0; i <= N; i++) {
if (X == Y && Y == Z)
break ;
let bit = (1 << i);
let cnt = chk(X, bit) + chk(Y, bit) + chk(Z, bit);
if (cnt == 0 || cnt == 3) {
ans = false ;
break ;
}
else if (cnt == 1) {
if (chk(X, bit) == 1)
X += bit;
else if (chk(Y, bit) == 1)
Y += bit;
else
Z += bit;
}
else {
if (chk(X, bit) == 0)
X += bit;
else if (chk(Y, bit) == 0)
Y += bit;
else
Z += bit;
}
count++;
}
return (ans && X == Y && Y == Z) ? count : -1;
}
let X = 1, Y = 6, Z = 7, N = 30;
document.write(minMove(X, Y, Z, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...