Leet Code Daily 2024-04-29 - Minimum Number of Operations to Make Array XOR Equal to K
Solution
- We need to flip each bit at most once.
- So the answer is the number of
1bits in .
Code
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
for (const auto& num : nums) {
k ^= num;
}
int ans = 0;
for (; k > 0; k >>= 1) {
ans += k & 1;
}
return ans;
}
};