Open In App

Find the original number by flipping K unique bits

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given three numbers X, Y and Z and an integer K. The number X is obtained by flipping K unique bits in the binary representation of N. Additionally, Y and Z are further obtained by flipping K unique bits in the binary representation of N. The task is to determine the original number N.

Examples:

Input: X = 9, Y = 17, Z = 29, K=1
Output: 25
Explanation: X = 01001, Y = 10001, Z = 11101
since k = 1 only one bit is flipped from the original number
To get X from N 5th position from left is flipped
To get Y from N 4th position from left is flipped
To get Z from N 3rd position from left is flipped
so, original number would be 25: 11001

Input: X = 11, Y = 16, Z = 61, K = 2
Output: 25

Approach: To solve the problem, follow the below idea:

The approach involves identifying the common set of bit positions flipped in generating two given numbers from the original number and then flipping those bits in one of the numbers to reconstruct the original number.

Step-by-step algorithm:

  • Define a helper function helper(X, Y) that takes two numbers x and y and returns a HashSet containing the positions where bits are flipped between X and Y.
  • In the findNumber() function:
    • Call helper(X, Y) and helper(X, Z) to obtain the sets of flipped bit positions between X and Y, and X and Z respectively.
    • Intersect these sets to find the common set of flipped bit positions and store it in set A.
    • Iterate over each position in set A and flip the corresponding bit in X.
    • Return the updated X as the original number.

Below is the implementation of the algorithm:

C++
#include <iostream>
#include <unordered_set>

using namespace std;

int findOriginalNumber(int X, int Y, int Z, int K);

// Helper function to find the flipped positions between two numbers
unordered_set<int> helper(int X, int Y);

int main()
{
    cout << findOriginalNumber(9, 17, 29, 1) << endl;
    return 0;
}

int findOriginalNumber(int X, int Y, int Z, int K)
{
    // Use helper function to find the flipped positions
    // in the binary representation of x and y
    unordered_set<int> A = helper(X, Y);

    // Use helper function to find the flipped positions
    // in the binary representation of x and z
    unordered_set<int> B = helper(X, Z);

    // Find the common flipped positions in both sets
    for (auto it = A.begin(); it != A.end();)
    {
        if (B.find(*it) == B.end())
        {
            it = A.erase(it);
        }
        else
        {
            ++it;
        }
    }

    // Flip the bits at the common positions to
    // reconstruct the original number
    for (int p : A)
    {
        int t = 1 << p;
        X = X ^ t;
    }

    return X;
}

// Helper function to find the flipped positions between
// two numbers
unordered_set<int> helper(int X, int Y)
{
    unordered_set<int> h;
    int ans = X ^ Y;

    // Find the positions where bits are flipped
    for (int i = 0; i < 32; i++)
    {
        if ((ans & 1) == 1)
        {
            h.insert(i);
        }
        ans /= 2;
    }

    return h;
}
Java
import java.util.HashSet;

public class Main {
    public static void main(String[] args)
    {
        System.out.println(
            findOriginalNumber(9, 17, 29, 1));
    }

    static int findOriginalNumber(int X, int Y, int Z,
                                  int K)
    {
        // Use helper function to find the flipped positions
        // in the binary representation of x and y
        HashSet&lt;Integer&gt; A = helper(X, Y);

        // Use helper function to find the flipped positions
        // in the binary representation of x and z
        HashSet&lt;Integer&gt; B = helper(X, Z);

        // Find the common flipped positions in both sets
        A.retainAll(B);

        // Flip the bits at the common positions to
        // reconstruct the original number
        for (int p : A) {
            int t = 1 &lt;&lt; p;
            X = X ^ t;
        }

        return X;
    }

    // Helper function to find the flipped positions between
    // two numbers
    static HashSet&lt;Integer&gt; helper(int X, int Y)
    {
        HashSet&lt;Integer&gt; h = new HashSet&lt;&gt;();
        int ans = X ^ Y;

        // Find the positions where bits are flipped
        for (int i = 0; i &lt; 32; i++) {
            if ((ans &amp; 1) == 1) {
                h.add(i);
            }
            ans /= 2;
        }

        return h;
    }
}
C#
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Console.WriteLine(FindOriginalNumber(9, 17, 29, 1));
    }

    static int FindOriginalNumber(int X, int Y, int Z, int K)
    {
        // Use helper function to find the flipped positions
        // in the binary representation of x and y
        HashSet<int> A = Helper(X, Y);

        // Use helper function to find the flipped positions
        // in the binary representation of x and z
        HashSet<int> B = Helper(X, Z);

        // Find the common flipped positions in both sets
        foreach (int item in new List<int>(A))
        {
            if (!B.Contains(item))
            {
                A.Remove(item);
            }
        }

        // Flip the bits at the common positions to
        // reconstruct the original number
        foreach (int p in A)
        {
            int t = 1 << p;
            X = X ^ t;
        }

        return X;
    }

    // Helper function to find the flipped positions between
    // two numbers
    static HashSet<int> Helper(int X, int Y)
    {
        HashSet<int> h = new HashSet<int>();
        int ans = X ^ Y;

        // Find the positions where bits are flipped
        for (int i = 0; i < 32; i++)
        {
            if ((ans & 1) == 1)
            {
                h.Add(i);
            }
            ans /= 2;
        }

        return h;
    }
}

// This code is contributed by shivamgupta0987654321
JavaScript
function findOriginalNumber(X, Y, Z, K) {
    // Helper function to find the flipped positions between two numbers
    function helper(X, Y) {
        let h = new Set();
        let ans = X ^ Y;

        // Find the positions where bits are flipped
        for (let i = 0; i < 32; i++) {
            if ((ans & 1) === 1) {
                h.add(i);
            }
            ans >>= 1;
        }

        return h;
    }

    // Use helper function to find the flipped positions
    // in the binary representation of x and y
    let A = helper(X, Y);

    // Use helper function to find the flipped positions
    // in the binary representation of x and z
    let B = helper(X, Z);

    // Find the common flipped positions in both sets
    for (let it of A) {
        if (!B.has(it)) {
            A.delete(it);
        }
    }

    // Flip the bits at the common positions to
    // reconstruct the original number
    for (let p of A) {
        let t = 1 << p;
        X = X ^ t;
    }

    return X;
}

console.log(findOriginalNumber(9, 17, 29, 1));
Python3
def find_original_number(X, Y, Z, K):
    # Helper function to find the flipped positions between two numbers
    def helper(a, b):
        h = set()
        ans = a ^ b

        # Find the positions where bits are flipped
        for i in range(32):
            if ans & 1 == 1:
                h.add(i)
            ans //= 2

        return h

    # Use helper function to find the flipped positions
    # in the binary representation of X and Y
    A = helper(X, Y)

    # Use helper function to find the flipped positions
    # in the binary representation of X and Z
    B = helper(X, Z)

    # Find the common flipped positions in both sets
    A.intersection_update(B)

    # Flip the bits at the common positions to
    # reconstruct the original number
    for p in A:
        t = 1 << p
        X ^= t

    return X

# Main function
if __name__ == "__main__":
    print(find_original_number(9, 17, 29, 1))

Output
25

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads