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:
nums[1] = 2 leaves [nums[0], nums[2]] = [1, 3].[1, 3] is 2, which equals target.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 <= 400 <= nums[i] <= 1040 <= target <= 104Solutions for this problem are being prepared.
Try solving it yourselfPractice Minimum Removals to Achieve Target XOR with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor