1787. Make the XOR of All Segments Equal to Zero Hard

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



1/**
2 * [1787] Make the XOR of All Segments Equal to Zero
3 *
4 * You are given an array nums​​​ and an integer k​​​​​. The <font face="monospace">XOR</font> of a segment [left, right] where left <= right is the XOR of all the elements with indices between left and right, inclusive: nums[left] XOR nums[left+1] XOR ... XOR nums[right].
5 * Return the minimum number of elements to change in the array such that the XOR of all segments of size k​​​​​​ is equal to zero.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [1,2,0,3,0], k = 1
10 * Output: 3
11 * Explanation: Modify the array from [<u>1</u>,<u>2</u>,0,<u>3</u>,0] to from [<u>0</u>,<u>0</u>,0,<u>0</u>,0].
12 * 
13 * Example 2:
14 * 
15 * Input: nums = [3,4,5,2,1,7,3,4,7], k = 3
16 * Output: 3
17 * Explanation: Modify the array from [3,4,<u>5</u>,<u>2</u>,<u>1</u>,7,3,4,7] to [3,4,<u>7</u>,<u>3</u>,<u>4</u>,7,3,4,7].
18 * 
19 * Example 3:
20 * 
21 * Input: nums = [1,2,4,1,2,5,1,2,6], k = 3
22 * Output: 3
23 * Explanation: Modify the array from [1,2,<u>4,</u>1,2,<u>5</u>,1,2,<u>6</u>] to [1,2,<u>3</u>,1,2,<u>3</u>,1,2,<u>3</u>].
24 *  
25 * Constraints:
26 * 
27 * 	1 <= k <= nums.length <= 2000
28 * 	​​​​​​0 <= nums[i] < 2^10
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero/
34// discuss: https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn min_changes(nums: Vec<i32>, k: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_1787() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.