Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 != target2Problem summary: 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) = target1 XOR(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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Dynamic Programming · Bit Manipulation
[2,3,1,4] 1 5
[1,0,0] 1 0
[7] 1 7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
class Solution {
public int alternatingXOR(int[] nums, int target1, int target2) {
final int mod = (int) 1e9 + 7;
Map<Integer, Integer> cnt1 = new HashMap<>();
Map<Integer, Integer> cnt2 = new HashMap<>();
cnt2.put(0, 1);
int ans = 0;
int pre = 0;
for (int x : nums) {
pre ^= x;
int a = cnt2.getOrDefault(pre ^ target1, 0);
int b = cnt1.getOrDefault(pre ^ target2, 0);
ans = (a + b) % mod;
cnt1.put(pre, (cnt1.getOrDefault(pre, 0) + a) % mod);
cnt2.put(pre, (cnt2.getOrDefault(pre, 0) + b) % mod);
}
return ans;
}
}
// Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
func alternatingXOR(nums []int, target1 int, target2 int) int {
mod := 1_000_000_007
cnt1 := make(map[int]int)
cnt2 := make(map[int]int)
cnt2[0] = 1
pre := 0
ans := 0
for _, x := range nums {
pre ^= x
a := cnt2[pre^target1]
b := cnt1[pre^target2]
ans = (a + b) % mod
cnt1[pre] = (cnt1[pre] + a) % mod
cnt2[pre] = (cnt2[pre] + b) % mod
}
return ans
}
# Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
class Solution:
def alternatingXOR(self, nums: List[int], target1: int, target2: int) -> int:
cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
cnt2[0] = 1
ans = pre = 0
mod = 10**9 + 7
for x in nums:
pre ^= x
a = cnt2[pre ^ target1]
b = cnt1[pre ^ target2]
ans = (a + b) % mod
cnt1[pre] = (cnt1[pre] + a) % mod
cnt2[pre] = (cnt2[pre] + b) % mod
return ans
// Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
// class Solution {
// public int alternatingXOR(int[] nums, int target1, int target2) {
// final int mod = (int) 1e9 + 7;
//
// Map<Integer, Integer> cnt1 = new HashMap<>();
// Map<Integer, Integer> cnt2 = new HashMap<>();
// cnt2.put(0, 1);
//
// int ans = 0;
// int pre = 0;
// for (int x : nums) {
// pre ^= x;
// int a = cnt2.getOrDefault(pre ^ target1, 0);
// int b = cnt1.getOrDefault(pre ^ target2, 0);
// ans = (a + b) % mod;
// cnt1.put(pre, (cnt1.getOrDefault(pre, 0) + a) % mod);
// cnt2.put(pre, (cnt2.getOrDefault(pre, 0) + b) % mod);
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3811: Number of Alternating XOR Partitions
function alternatingXOR(nums: number[], target1: number, target2: number): number {
const MOD = 1_000_000_007;
const cnt1 = new Map<number, number>();
const cnt2 = new Map<number, number>();
cnt2.set(0, 1);
let pre = 0;
let ans = 0;
for (const x of nums) {
pre ^= x;
const a = cnt2.get(pre ^ target1) ?? 0;
const b = cnt1.get(pre ^ target2) ?? 0;
ans = (a + b) % MOD;
cnt1.set(pre, ((cnt1.get(pre) ?? 0) + a) % MOD);
cnt2.set(pre, ((cnt2.get(pre) ?? 0) + b) % MOD);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.