Skip to main content

Minimum Removals to Achieve Target XOR - Solution & Explanation

Practice this problem

Problem Statement

You are given an integer array nums and an integer target.

You may remove any number of elements from nums (possibly zero).

Return the minimum number of removals required so that the bitwise XOR of the remaining elements equals target. If it is impossible to achieve target, return -1.

The bitwise XOR of an empty array is 0.

 

Example 1:

Input: nums = [1,2,3], target = 2

Output: 1

Explanation:

  • Removing nums[1] = 2 leaves [nums[0], nums[2]] = [1, 3].
  • The XOR of [1, 3] is 2, which equals target.
  • It is not possible to achieve XOR = 2 in less than one removal, therefore the answer is 1.

Example 2:

Input: nums = [2,4], target = 1

Output: -1

Explanation:

It is impossible to remove elements to achieve target. Thus, the answer is -1.

Example 3:

Input: nums = [7], target = 7

Output: 0

Explanation:

The XOR of all elements is nums[0] = 7, which equals target. Thus, no removal is needed.

 

Constraints:

  • 1 <= nums.length <= 40
  • 0 <= nums[i] <= 104
  • 0 <= target <= 104

Approach Overview

Problem Overview: Given an array of integers and a target value, remove the minimum number of elements so the XOR of the remaining elements equals the target. The task reduces to deciding which elements to remove while preserving the desired XOR.

Approach 1: Brute Force Subset Enumeration (O(2^n) time, O(1) space)

Try every subset of elements to remove. For each subset, compute the XOR of the remaining elements and check whether it equals the target. Track the minimum removal count across all valid subsets. This works because every element has two choices: keep or remove. The method is straightforward but infeasible once n grows beyond ~20 since it requires evaluating 2^n possibilities.

Approach 2: XOR Transformation + Hash Map Dynamic Programming (O(n * U) time, O(U) space)

Let S be the XOR of the entire array. If you remove a subset with XOR R, the remaining XOR becomes S ^ R. To achieve the target, the removed subset must satisfy R = S ^ target. The problem becomes finding a subset whose XOR equals this value while minimizing the number of removed elements.

Use dynamic programming with a hash map: dp[x] stores the minimum removals needed to achieve XOR x using processed elements. Start with dp[0] = 0. For each number, update states by either skipping it (keep element) or including it in the removed subset (XOR transition x ^ num with cost +1). A hash map keeps only reachable XOR states. After processing all elements, the answer is dp[S ^ target] if it exists.

This technique relies on properties of bit manipulation and XOR symmetry. The number of distinct XOR states U is typically small compared with 2^n, making the approach efficient in practice.

Approach 3: DP with State Compression (O(n * U) time, O(U) space)

The same idea can be implemented with rolling maps to avoid overwriting states during iteration. For each number, clone the current map and update new XOR states derived from removing that number. This pattern mirrors classic dynamic programming over subsets but stores only reachable XOR values rather than full bitmasks.

Recommended for interviews: The XOR transformation plus hash map DP is the expected solution. Explaining the identity S ^ R = target shows understanding of XOR algebra. Brute force demonstrates the baseline reasoning, but the DP approach proves you can optimize exponential subset search using state compression.

Approach 1: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 2: Dynamic Programming

We define a 2D array f, where f[i][j] represents the maximum number of elements we can select from the first i elements such that their XOR sum equals j. Initially, f[0][0] = 0 and all other f[0][j] are negative infinity.

For each element nums[i - 1], we can choose not to use it, in which case f[i][j] equals f[i - 1][j]; or we can choose to use it, in which case f[i][j] equals f[i - 1][j \oplus nums[i - 1]] + 1. Thus, the transition equation is:

$ \begin{aligned} f[i][j] = max(f[i - 1][j], f[i - 1][j \oplus nums[i - 1]] + 1) \end{aligned}

Finally, if f[n][target] is less than 0, it means the target XOR value cannot be achieved, and we return -1; otherwise, we return n - f[n][target], which is the number of elements that need to be removed.

The time complexity is O(n times 2^m) and the space complexity is O(n times 2^m), where n is the length of the array and m$ is the number of binary bits of the maximum element in the array.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Default Approach
Dynamic Programming

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subset EnumerationO(2^n)O(1)Small arrays (n ≤ 20) or for conceptual understanding
Hash Map XOR Dynamic ProgrammingO(n * U)O(U)General case; efficiently tracks reachable XOR states
Rolling Map State CompressionO(n * U)O(U)Cleaner DP implementation that avoids overwriting states

Video Solution

Leetcode 3877 | Minimum Removals to Achieve Target XOR | Leetcode weekly contest 494CodeWithMeGuys893 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Minimum Removals to Achieve Target XOR easy or hard?
The problem is typically rated Medium because the brute-force idea is simple but recognizing the XOR transformation and applying dynamic programming over XOR states requires deeper insight.
Minimum Removals to Achieve Target XOR Python/Java solution
Implement the DP using a dictionary or HashMap where dp[x] tracks the minimum removals for XOR x. For each number, update transitions to x ^ num with cost +1 while preserving existing states. The same logic translates directly across Python, Java, C++, Go, and TypeScript.
How to solve Minimum Removals to Achieve Target XOR in O(n)?
A strict O(n) algorithm is generally not possible because the solution must track multiple XOR states. The closest practical optimization uses a hash map DP that processes each element once while updating reachable XOR values, giving O(n * U) complexity with small state growth.
What is the best approach for Minimum Removals to Achieve Target XOR?
The most effective approach uses XOR transformation plus hash map dynamic programming. Compute the XOR of the entire array S, then find the minimum-size subset whose XOR equals S ^ target. A DP map tracks the minimum removals needed for each XOR state. This runs in O(n * U) time where U is the number of reachable XOR states.
Is Minimum Removals to Achieve Target XOR asked at Google/Amazon/Meta?
XOR-based subset and dynamic programming problems frequently appear in interviews at companies like Google, Amazon, and Meta. Variants involving subset XOR, prefix XOR, or minimizing operations are common because they test bit manipulation and DP reasoning.
What data structure is used in Minimum Removals to Achieve Target XOR?
A hash map is used to store dynamic programming states where the key represents a reachable XOR value and the value stores the minimum number of removals needed to achieve it. The solution also relies on bit manipulation properties of the XOR operation.
What is the time complexity of Minimum Removals to Achieve Target XOR?
The optimized solution runs in O(n * U) time and O(U) space, where U is the number of distinct XOR values generated during DP transitions. In the worst case U can approach 2^k for k-bit numbers, but in practice it remains much smaller than 2^n.

Ready to solve this problem?

Practice Minimum Removals to Achieve Target XOR with our built-in code editor and test cases.

Practice on FleetCode