Make the XOR of All Segments Equal to Zero - Problem
Make the XOR of All Segments Equal to Zero

You are given an array nums and an integer k. Your goal is to modify the minimum number of elements in the array so that every segment of size k has XOR equal to zero.

A segment [left, right] where left <= right is defined as the XOR of all elements with indices between left and right, inclusive: nums[left] XOR nums[left+1] XOR ... XOR nums[right].

Example: If nums = [3,4,5,2,1,7,3,4,1] and k = 3, then segments of size 3 are:
[3,4,5] → XOR = 3⊕4⊕5 = 2
[4,5,2] → XOR = 4⊕5⊕2 = 3
[5,2,1] → XOR = 5⊕2⊕1 = 6
• And so on...

Your task is to find the minimum number of changes needed to make all these segment XORs equal to zero.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,2,0,3,0], k = 4
Output: 2
💡 Note: We need to make segments [1,2,0,3] and [2,0,3,0] both have XOR = 0. Currently [1,2,0,3] has XOR = 1^2^0^3 = 0 (already good), and [2,0,3,0] has XOR = 2^0^3^0 = 1. We can change one element in each overlapping segment to make both XOR to 0.
example_2.py — Small Array
$ Input: nums = [3,4,5,2,1,7,3,4,1], k = 3
Output: 3
💡 Note: For k=3, we have segments [3,4,5], [4,5,2], [5,2,1], [2,1,7], [1,7,3], [7,3,4], [3,4,1]. We need to modify minimum elements so all segments XOR to 0. The cyclic pattern requires elements at positions 0,3,6 to follow one pattern, 1,4,7 another, and 2,5,8 another.
example_3.py — Edge Case
$ Input: nums = [1,2,3,4], k = 4
Output: 1
💡 Note: Only one segment [1,2,3,4] with XOR = 1^2^3^4 = 4. We need to change exactly one element to make XOR = 0. We can change any element to make the total XOR equal to 0.

Constraints

  • 1 ≤ nums.length ≤ 2000
  • 1 ≤ nums[i] ≤ 1023
  • 1 ≤ k ≤ nums.length
  • All segments of size k must have XOR equal to zero

Visualization

Tap to expand
Cyclic Pattern in XOR Segments0123012Segment 1 (k=4)Segment 2 (k=4)Pattern Groups:Group 0Group 1Group 2Group 3Each group must be set optimally to minimize total changesOnce k groups are fixed, the entire array pattern is determined!
Understanding the Visualization
1
Identify Cyclic Groups
Elements at positions 0,k,2k belong to group 0; 1,k+1,2k+1 to group 1, etc.
2
Count Frequencies
For each group, count how often each value appears - this helps minimize changes
3
Dynamic Programming
Build optimal solution incrementally: dp[pos][xor] = minimum changes to achieve XOR=xor at position pos
4
Find Optimal Answer
The answer is dp[k][0] - minimum changes needed so that all k positions combine to XOR=0
Key Takeaway
🎯 Key Insight: The problem has a beautiful cyclic structure - elements repeat in groups of size k, so we only need to optimize k independent groups using dynamic programming!
Asked in
Google 45 Meta 32 Microsoft 28 Amazon 22
28.6K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen