Achieving Target XOR

You are given an integer array nums and an integer target. You can remove any number of elements from the array (including zero elements, or all elements). Your goal is to make the bitwise XOR of the remaining elements exactly equal to target.

Return the minimum number of removals required. If it's mathematically impossible, return -1. Your task is to remove the minimum number of elements to hit the XOR target.

Example 1:

Input: nums = [5, 7, 3, 1], target = 4

Output: 2

Explanation: The bitwise XOR of all elements in the array is 5 ^ 7 ^ 3 ^ 1 = 0, which does not equal our target. To achieve the target, we can remove exactly two elements. For instance, if we remove 5 and 1, the remaining array is [7, 3], and their XOR is 7 ^ 3 = 4. Alternatively, removing 7 and 3 leaves [5, 1], whose XOR is also 5 ^ 1 = 4. It is impossible to achieve an XOR of 4 by making fewer than 2 removals, making 2 the minimum.

Example 2:

Input: nums = [10, 20, 30], target = 0

Output: 0

Explanation: The bitwise XOR of all elements in the initial array evaluates to 10 ^ 20 ^ 30 = 0. Since the starting array's XOR already perfectly matches the target value of 0, we are not required to make any modifications. Therefore, the minimum number of removals is simply 0.

Now Your Turn!

Pick the correct output for the given input

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

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int minRemovals(vector<int>& nums, int target) {
// Write your code here
return 0;
}
};
Ā 
Test Case

Input:

Nums
Target