Skip to content
Related Articles

Related Articles

Count of operations to make numbers equal by adding power of 2

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jul, 2022
View Discussion
Improve Article
Save Article

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++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the bit is set or not
int chk(int n, int bit) { return ((n & bit) != 0) ? 1 : 0; }
 
// Function to find
// the minimum number of moves
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;
 
        // Left shifting of bit
        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;
}
 
// Driver code
int main()
{
    int X = 1, Y = 6, Z = 7, N = 30;
 
    // Function call
    cout << minMove(X, Y, Z, N);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java




// Java code to implement the approach
 
import java.io.*;
 
class GFG {
     
    // Function to check if the bit is set or not
    static int chk(int n, int bit)
    {
        return ((n & bit) != 0) ? 1 : 0;
    }
   
    // Function to find
    // the minimum number of moves
    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;
 
            // Left shifting of bit
            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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int X = 1, Y = 6, Z = 7, N = 30;
 
        // Function call
        System.out.println(minMove(X, Y, Z, N));
    }
}


Python3




# Python code to implement the approach
 
# Function to check if the bit is set or not
def chk(n, bit):
    return 1 if (n & bit) != 0 else 0
 
# Function to find the minimum number of moves
def minMove(X, Y, Z, N):
    count = 0
    ans = True
    for i in range(N+1):
 
        if X == Y and Y == Z:
            break
 
        # Left shifting of bit
        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
 
# Driver Code
if __name__ == '__main__':
    X = 1
    Y = 6
    Z = 7
    N = 30
    # Function call
    print(minMove(X, Y, Z, N))
 
# This code is contributed by Tapesh(tapeshdua420)


C#




// C# code to implement the approach
 
using System;
 
public class GFG {
     
    // Function to check if the bit is set or not
    static int chk(int n, int bit)
    {
        return ((n & bit) != 0) ? 1 : 0;
    }
   
    // Function to find
    // the minimum number of moves
    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;
 
            // Left shifting of bit
            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;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int X = 1, Y = 6, Z = 7, N = 30;
 
        // Function call
        Console.WriteLine(minMove(X, Y, Z, N));
    }
}
 
// This code contributed by shikhasingrajput


Javascript




<script>
// Javascript code to implement the approach
 
// Function to check if the bit is set or not
function chk(n,bit) { return ((n & bit) != 0) ? 1 : 0; }
 
// Function to find
// the minimum number of moves
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;
 
        // Left shifting of bit
        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;
}
 
// Driver code
    let X = 1, Y = 6, Z = 7, N = 30;
 
    // Function call
    document.write(minMove(X, Y, Z, N));
 
// This code is contributed by satwik4409.
    </script>


Output

3

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!