Minimum Bitwise XOR operations to make any two array elements equal
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 :0
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
- The answer will be 0, if there are already equal elements in the array.
- 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.
- 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.
- If any of the above conditions is not satisfied then the answer will be -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int a[], int n, int K)
{
unordered_map< int , bool > map;
for ( int i = 0; i < n; i++) {
if (map[a[i]])
return 0;
map[a[i]] = true ;
}
int b[n];
for ( int i = 0; i < n; i++)
b[i] = a[i] ^ K;
map.clear();
for ( int i = 0; i < n; i++) {
if (a[i] != b[i])
map[b[i]] = true ;
}
for ( int i = 0; i < n; i++)
if (map[a[i]])
return 1;
map.clear();
for ( int i = 0; i < n; i++) {
if (map[b[i]])
return 2;
map[b[i]] = true ;
}
return -1;
}
int main()
{
int K = 3;
int a[] = { 1, 9, 4, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << minOperations(a, n, K);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
static int minOperations( int [] a, int n, int k)
{
HashMap<Integer,
Boolean> map = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (map.containsKey(a[i]) &&
map.get(a[i]))
return 0 ;
map.put(a[i], true );
}
int [] b = new int [n];
for ( int i = 0 ; i < n; i++)
b[i] = a[i] ^ k;
map.clear();
for ( int i = 0 ; i < n; i++)
{
if (a[i] != b[i])
map.put(b[i], true );
}
for ( int i = 0 ; i < n; i++)
if (map.containsKey(a[i]) &&
map.get(a[i]))
return 1 ;
map.clear();
for ( int i = 0 ; i < n; i++)
{
if (map.containsKey(b[i]) &&
map.get(b[i]))
return 2 ;
map.put(b[i], true );
}
return - 1 ;
}
public static void main(String[] args)
{
int K = 3 ;
int [] a = { 1 , 9 , 4 , 3 };
int n = a.length;
System.out.println(minOperations(a, n, K));
}
}
|
Python3
def minOperations(a, n, K) :
map = dict .fromkeys(a, False );
for i in range (n) :
if ( map [a[i]]) :
return 0 ;
map [a[i]] = True ;
b = [ 0 ] * n;
for i in range (n) :
b[i] = a[i] ^ K;
map .clear();
for i in range (n) :
if (a[i] ! = b[i]) :
map [b[i]] = True ;
for i in range (n) :
if a[i] in map :
return 1 ;
map .clear();
for i in range (n) :
if b[i] in map :
return 2 ;
map [b[i]] = True ;
return - 1 ;
if __name__ = = "__main__" :
K = 3 ;
a = [ 1 , 9 , 4 , 3 ];
n = len (a);
print (minOperations(a, n, K));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int minOperations( int [] a,
int n, int K)
{
Dictionary< int , Boolean> map =
new Dictionary< int , Boolean>();
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(a[i]))
return 0;
map.Add(a[i], true );
}
int [] b = new int [n];
for ( int i = 0; i < n; i++)
b[i] = a[i] ^ K;
map.Clear();
for ( int i = 0; i < n; i++)
{
if (a[i] != b[i])
map.Add(b[i], true );
}
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(a[i]))
return 1;
}
map.Clear();
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(b[i]))
return 2;
map.Add(b[i], true );
}
return -1;
}
public static void Main(String[] args)
{
int K = 3;
int [] a = { 1, 9, 4, 3 };
int n = a.Length;
Console.WriteLine(minOperations(a, n, K));
}
}
|
Javascript
<script>
function minOperations(a, n, K)
{
var map = new Map();
for ( var i = 0; i < n; i++) {
if (map[a[i]])
return 0;
map[a[i]] = true ;
}
var b = Array(n);
for ( var i = 0; i < n; i++)
b[i] = a[i] ^ K;
map = new Map();
for ( var i = 0; i < n; i++) {
if (a[i] != b[i])
map[b[i]] = true ;
}
for ( var i = 0; i < n; i++)
if (map[a[i]])
return 1;
map = new Map();
for ( var i = 0; i < n; i++) {
if (map[b[i]])
return 2;
map[b[i]] = true ;
}
return -1;
}
var K = 3;
var a = [ 1, 9, 4, 3 ];
var n = a.length;
document.write( minOperations(a, n, K));
</script>
|
Time complexity: O(n) where n is size of input array
Auxiliary space: O(n)
Last Updated :
05 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...