Backtracking · Medium
A bounded configuration exercise selects any subset of available mask toggles. Each toggle may be selected once, and two identical toggles still represent distinct choices. The tool needs the number of selections producing an exact final XOR mask.
Count index subsets whose combined XOR equals the target, including the empty selection when appropriate.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: unsigned32[], target: unsigned32 }
Fields
- nums contains unsigned 32-bit integers from 0 through 4294967295. Values with bit 31 set are positive JSON integers.
- target is an unsigned 32-bit integer from 0 through 4294967295.
Output
Return value
- Return the number of subsets of indexes. Different index choices count separately even if values repeat.
- The empty subset has XOR zero. With no inputs, return one for target zero and zero otherwise.
Constraints
- 0 <= nums.length <= 20. The largest answer is 1048576.
Examples
Example 1
{
"nums": [
1,
1
],
"target": 0
}Returns
2Example 2
{
"nums": [
2147483648,
0
],
"target": 2147483648
}Returns
2Required complexity
O(2ⁿ) time and O(n) recursion space