Given an integer array, we can flip one bit of one element of the array at every step, we asked to compute the minimum flips, such that the XOR of all elements of the array is equal to the given integer $k$.
Intuition
XOR of multiple bits is always equal to either 1
or 0
, and changes one of the input bits will cause the result change to the other one.
Approach
We first compute the XOR of all elements of the array, then we compare bit by bit with the XOR result and the given k
, utils all bits becomes the same.
Complexity
Time complexity: compute the XOR result $$O(n)$$
Space complexity: No extra spaces are needed. $$O(1)$$
Code
|
|