You are given an integer array nums and two distinct integers target1 and target2.
A partition of nums splits it into one or more contiguous, non-empty blocks that cover the entire array without overlap.
A partition is valid if the bitwise XOR of elements in its blocks alternates between target1 and target2, starting with target1.
Formally, for blocks b1, b2, …:
XOR(b1) = target1XOR(b2) = target2 (if it exists)XOR(b3) = target1, and so on.Return the number of valid partitions of nums, modulo 109 + 7.
Note: A single block is valid if its XOR equals target1.
Example 1:
Input: nums = [2,3,1,4], target1 = 1, target2 = 5
Output: 1
Explanation:
[2, 3] is 1, which matches target1.[1, 4] is 5, which matches target2.Example 2:
Input: nums = [1,0,0], target1 = 1, target2 = 0
Output: 3
Explanation:
[1, 0, 0] is 1, which matches target1.[1] and [0, 0] are 1 and 0, matching target1 and target2.[1, 0] and [0] are 1 and 0, matching target1 and target2.Example 3:
Input: nums = [7], target1 = 1, target2 = 7
Output: 0
Explanation:
[7] is 7, which does not match target1, so no valid partition exists.
Constraints:
1 <= nums.length <= 1050 <= nums[i], target1, target2 <= 105target1 != target2Solutions for this problem are being prepared.
Try solving it yourselfPractice Number of Alternating XOR Partitions with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor