Make the XOR of All Segments Equal to Zero

You are given an array nums and an integer k. The XOR 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].

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.

Example 1
Inputnums = [1,2,0,3,0], k = 1
Output3
Changing the array from [1, 2, 0, 3, 0] to [0, 0, 0, 0, 0] requires 3 changes and makes every segment of size 1 have XOR 0.
Example 2
Inputnums = [3,4,5,2,1,7,3,4,7], k = 3
Output3
Changing the highlighted elements transforms the array to [3, 4, 7, 3, 4, 7, 3, 4, 7], requiring 3 changes so every segment of size 3 has XOR 0.

Constraints

  • 1 <= k <= nums.length <= 2000
  • 0 <= nums[i] < 2^10

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate