Minimum Bitwise AND operations to make any two array elements equal

Given an array of integers of size ‘n’ and an integer ‘k’,
We can perform the Bitwise AND 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 : k = 6 ; Array : 1, 2, 1, 2
Output : 0
Explanation : There are already two equal elements in the array so the answer is 0.

Input : k = 2 ; Array : 5, 6, 2, 4
Output : 1
Explanation : If we apply AND operation on element ‘6’, it will become 6&2 = 2
And the array will become 5 2 2 4,
Now, the array has two equal elements, so the answer is 1.

Input : k = 15 ; Array : 1, 2, 3
Output : -1
Explanation : No matter how many times we perform the above mentioned operation,
this array will never have equal element pair. So the answer is -1



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, no matter how many times you perform (y&k)
it’ll always give ‘y’ as the result.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the
// 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 AND 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 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 AND operations
    return -1;
}
  
// Driver code
int main()
{
  
    int K = 3;
    int a[] = { 1, 2, 3, 7 };
    int n = sizeof(a)/sizeof(a[0]);
  
    // Function call to compute the result
    cout << minOperations(a, n, K);
  
    return 0;
}

chevron_right


Python3

# Python3 implementation of the approach
from collections import defaultdict

# Function to count the minimum
# operations required.
def minOperations(a, n, K):

Map = defaultdict(lambda:False)
for i in range(0, n):

# check if the initial array
# already contains an equal pair
if Map[a[i]] == True:
return 0
Map[a[i]] = True

# create new array with AND operations
b = []
for i in range(0, n):
b.append(a[i] & K)

# clear the map
Map.clear()

# Check if the solution
# is a single operation
for i in range(0, n):

# If Bitwise 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 i in range(0, n):

# Single operation
# will be enough
if Map[a[i]] == True:
return 1

# clear the map
Map.clear()

# Check if the solution
# is two operations
for i in range(0, n):

# Check if the array ‘b’
# contains duplicates
if Map[b[i]] == True:
return 2

Map[b[i]] = True

# otherwise it is impossible to
# create such an array with
# Bitwise AND operations
return -1

# Driver code
if __name__ == “__main__”:

K = 3
a = [1, 2, 3, 7]
n = len(a)

# Function call to compute the result
print(minOperations(a, n, K))

# This code is contributed by Rituraj Jain

Output:

1

Complexity: O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rituraj_jain



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.