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’.
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
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Minimum number of increment-other operations to make all array elements equal.
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimum increment by k operations to make all elements equal
- Minimum distance between any two equal elements in an Array
- Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same
- Find the number of operations required to make all array elements Equal
- Find if it is possible to make all elements of an array equal by the given operations
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.