Problem solution · C++

Minimum Removals to Achieve Target Xor

Minimum Removals to Achieve Target Xor: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
59 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Minimum Removals to Achieve Target Xor, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 59 lines of C++ from the credited upstream file minimum-removals-to-achieve-target-xor.cpp.
  • The implementation visibly relies on sequence storage, hash lookup, cached states.
  • 6 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Removals to Achieve Target Xor · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n * r), r = max(nums)// Space: O(r) // bitmasks, bfsclass Solution {public:    int minRemovals(vector<int>& nums, int target) {        const auto& bfs = [&]() {            unordered_map<int, int> dist;            dist[0] = 0;            vector<int> q = {0};            while (!empty(q)) {                vector<int> new_q;                for (const auto& k : q) {                    if (k == target) {                        return dist[k];                    }                    for (const auto& x : nums) {                        if (dist.count(k ^ x)) {                            continue;                        }                        dist[k ^ x] = dist[k] + 1;                        new_q.emplace_back(k ^ x);                    }                }                q = move(new_q);            }            return -1;        };         for (const auto& x : nums) {            target ^= x;        }        return bfs();    }}; // Time:  O(n * r), r = max(nums)// Space: O(r)// bitmasks, dpclass Solution2 {public:    int minRemovals(vector<int>& nums, int target) {        unordered_map<int, int> dp;        dp[0] = 0;        for (const auto& x : nums) {            target ^= x;            unordered_map<int, int> new_dp(dp);            for (const auto& [k, _] : dp) {                if (!new_dp.count(k ^ x) || new_dp[k ^ x] > dp[k] + 1) {                    new_dp[k ^ x] = dp[k] + 1;                }            }            dp = move(new_dp);        }        return dp.count(target) ? dp[target] : -1;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗