Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    2997 - Minimum Number of Operations to Make Array XOR Equal to K.

    Medium

    You are given a 0-indexed integer array nums and a positive integer k.

    You can apply the following operation on the array any number of times:

    • Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.

    Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.

    Note that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.

    Example 1:

    Input: nums = 2,1,3,4, k = 1

    Output: 2

    Explanation: We can do the following operations:

    • Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes 2,1,2,4.

    • Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes 6,1,2,4.

    The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.

    It can be shown that we cannot make the XOR equal to k in less than 2 operations.

    Example 2:

    Input: nums = 2,0,2,0, k = 0

    Output: 0

    Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.

    Constraints:

    • <code>1 <= nums.length <= 10<sup>5</sup></code>

    • <code>0 <= numsi<= 10<sup>6</sup></code>

    • <code>0 <= k <= 10<sup>6</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer minOperations(IntArray nums, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait