Talentd
FleetCode

3877. Minimum Removals to Achieve Target XOR

Medium41.2% acceptance

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

Hints open one at a time

Hint 1
Use meet in the middle or dynamic programming
14:06
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minRemovals(self, nums: List[int], target: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
2
Input
[2,4]
1
Input
[7]
7
Your code is saved on this device.Solution