aboutsummaryrefslogtreecommitdiff
path: root/min_num_opp_arr_equal_xor.py
blob: 79bd53a7d3088894f43590157a241a73327c75e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 2997. Minimum Number of Operations to Make Array XOR Equal to K

"""
given a 0-indexed 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 meops
chaning a 0 to a 1 or vice versa. return the minimum number of operations
required to make the bitwise xor of all elements in the array equal to 'k'.
note that you can flip leading zero bits in the binary representation of the
elements. for example, for the number of '101_2' you can flip the fouth bit
and obtain '1101_2'
"""


class Solution(object):
    def minOperations(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        cnt = k
        for n in nums:
            cnt ^= n
        ops = 0
        while cnt:
            ops += cnt & 1
            cnt >>= 1
        return ops


if __name__ == "__main__":
    obj = Solution()
    print(obj.minOperations(nums=[2, 1, 3, 4], k=1))
    print(obj.minOperations(nums=[2, 0, 2, 0], k=0))