../ Competitive Programming
-rw-r--r--aguang627 B1 min read

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 1 bits in ⊕i=0n−1numsi⊕k\oplus_{i=0}^{n - 1} \text{nums}_i \oplus k.

Code

c++
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;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window