3811. Number of Alternating XOR Partitions Medium

@problem@discussion
#Array#Hash Table#Dynamic Programming#Bit Manipulation



1/**
2 * [3811] Number of Alternating XOR Partitions
3 *
4 * You are given an integer array nums and two distinct integers target1 and target2.
5 * A partition of nums splits it into one or more contiguous, non-empty blocks that cover the entire array without overlap.
6 * A partition is valid if the bitwise XOR of elements in its blocks alternates between target1 and target2, starting with target1.
7 * Formally, for blocks b1, b2, …:
8 * 
9 * 	XOR(b1) = target1
10 * 	XOR(b2) = target2 (if it exists)
11 * 	XOR(b3) = target1, and so on.
12 * 
13 * Return the number of valid partitions of nums, modulo 10^9 + 7.
14 * Note: A single block is valid if its XOR equals target1.
15 *  
16 * <strong class="example">Example 1:
17 * <div class="example-block">
18 * Input: <span class="example-io">nums = [2,3,1,4], target1 = 1, target2 = 5</span>
19 * Output: <span class="example-io">1</span>
20 * Explanation:​​​​​​​
21 * 
22 * 	The XOR of [2, 3] is 1, which matches target1.
23 * 	The XOR of the remaining block [1, 4] is 5, which matches target2.
24 * 	This is the only valid alternating partition, so the answer is 1.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">nums = [1,0,0], target1 = 1, target2 = 0</span>
29 * Output: <span class="example-io">3</span>
30 * Explanation:
31 * 
32 * 	​​​​​​​The XOR of [1, 0, 0] is 1, which matches target1.
33 * 	The XOR of [1] and [0, 0] are 1 and 0, matching target1 and target2.
34 * 	The XOR of [1, 0] and [0] are 1 and 0, matching target1 and target2.
35 * 	Thus, the answer is 3.​​​​​​​
36 * </div>
37 * <strong class="example">Example 3:
38 * <div class="example-block">
39 * Input: <span class="example-io">nums = [7], target1 = 1, target2 = 7</span>
40 * Output: <span class="example-io">0</span>
41 * Explanation:
42 * 
43 * 	The XOR of [7] is 7, which does not match target1, so no valid partition exists.
44 * </div>
45 *  
46 * Constraints:
47 * 
48 * 	1 <= nums.length <= 10^5
49 * 	0 <= nums[i], target1, target2 <= 10^5
50 * 	target1 != target2
51 * 
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/number-of-alternating-xor-partitions/
56// discuss: https://leetcode.com/problems/number-of-alternating-xor-partitions/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61    pub fn alternating_xor(nums: Vec<i32>, target1: i32, target2: i32) -> i32 {
62        0
63    }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_3811() {
74    }
75}
76

Back
© 2026 bowen.ge All Rights Reserved.