Open In App

Minimum Bitwise XOR operations to make any two array elements equal

Last Updated : 05 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two elements of the array equal after performing the above-mentioned operation then print -1.
Examples: 
 

Input : arr[] = {1, 9, 4, 3}, K = 3 
Output :-1 
Explanation : No possible to make any two elements equal
Input : arr[] = {13, 13, 21, 15}, K = 13 
Output :
Explanation : Already exists two same elements


 


Approach: The key observation is that if it is possible to make the desired array then the answer will be either 0, 1 or 2. It will never exceed 2. 
 

Because, if (x ^ k) = y 
then, performing (y ^ k) will give x again 
 


 

  1. The answer will be 0, if there are already equal elements in the array.
  2. For the answer to be 1, we will create a new array b[] which holds b[i] = (a[i] ^ K)
    Now, for each a[i] we will check if there is any index j such that i != j and a[i] = b[j].
    If yes, then the answer will be 1.
  3. For the answer to be 2, we will check for an index i in the new array b[], If there is any index j such that i != j and b[i] = b[j]
    If yes, then the answer will be 2.
  4. If any of the above conditions is not satisfied then the answer will be -1.


Below is the implementation of the above approach: 
 

C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Function to return the count of
// minimum operations required
int minOperations(int a[], int n, int K)
{
    unordered_map<int, bool> map;
    for (int i = 0; i < n; i++) {

        // Check if the initial array
        // already contains an equal pair
        if (map[a[i]])
            return 0;
        map[a[i]] = true;
    }

    // Create new array with XOR operations
    int b[n];
    for (int i = 0; i < n; i++)
        b[i] = a[i] ^ K;

    // Clear the map
    map.clear();

    // Check if the solution
    // is a single operation
    for (int i = 0; i < n; i++) {

        // If Bitwise XOR operation between
        // 'k' and a[i] gives
        // a number other than a[i]
        if (a[i] != b[i])
            map[b[i]] = true;
    }

    // Check if any of the a[i]
    // gets equal to any other element
    // of the array after the operation
    for (int i = 0; i < n; i++)

        // Single operation
        // will be enough
        if (map[a[i]])
            return 1;

    // Clear the map
    map.clear();

    // Check if the solution
    // is two operations
    for (int i = 0; i < n; i++) {

        // Check if the array 'b'
        // contains duplicates
        if (map[b[i]])
            return 2;

        map[b[i]] = true;
    }

    // Otherwise it is impossible to
    // create such an array with
    // Bitwise XOR operations
    return -1;
}

// Driver code
int main()
{

    int K = 3;
    int a[] = { 1, 9, 4, 3 };
    int n = sizeof(a) / sizeof(a[0]);

    // Function call to compute the result
    cout << minOperations(a, n, K);

    return 0;
}
Java Python3 C# JavaScript

Output: 
-1

 

Time complexity: O(n) where n is size of input array

Auxiliary space: O(n)


Article Tags :
Practice Tags :

Similar Reads