|
| 1 | +# [1787. Make the XOR of All Segments Equal to Zero](https://leetcode.com/problems/make-the-xor-of-all-segments-equal-to-zero) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given an array <code>nums</code> and an integer <code>k</code>. The <font face="monospace">XOR</font> of a segment <code>[left, right]</code> where <code>left <= right</code> is the <code>XOR</code> of all the elements with indices between <code>left</code> and <code>right</code>, inclusive: <code>nums[left] XOR nums[left+1] XOR ... XOR nums[right]</code>.</p> |
| 6 | + |
| 7 | +<p>Return <em>the minimum number of elements to change in the array </em>such that the <code>XOR</code> of all segments of size <code>k</code> is equal to zero.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> nums = [1,2,0,3,0], k = 1 |
| 13 | +<strong>Output:</strong> 3 |
| 14 | +<strong>Explanation: </strong>Modify the array from [<u><strong>1</strong></u>,<u><strong>2</strong></u>,0,<u><strong>3</strong></u>,0] to from [<u><strong>0</strong></u>,<u><strong>0</strong></u>,0,<u><strong>0</strong></u>,0]. |
| 15 | +</pre> |
| 16 | + |
| 17 | +<p><strong class="example">Example 2:</strong></p> |
| 18 | + |
| 19 | +<pre><strong>Input:</strong> nums = [3,4,5,2,1,7,3,4,7], k = 3 |
| 20 | +<strong>Output:</strong> 3 |
| 21 | +<strong>Explanation: </strong>Modify the array from [3,4,<strong><u>5</u></strong>,<strong><u>2</u></strong>,<strong><u>1</u></strong>,7,3,4,7] to [3,4,<strong><u>7</u></strong>,<strong><u>3</u></strong>,<strong><u>4</u></strong>,7,3,4,7]. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 3:</strong></p> |
| 25 | + |
| 26 | +<pre><strong>Input:</strong> nums = [1,2,4,1,2,5,1,2,6], k = 3 |
| 27 | +<strong>Output:</strong> 3 |
| 28 | +<strong>Explanation: </strong>Modify the array from [1,2,<strong><u>4,</u></strong>1,2,<strong><u>5</u></strong>,1,2,<strong><u>6</u></strong>] to [1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>,1,2,<strong><u>3</u></strong>].</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= k <= nums.length <= 2000</code></li> |
| 35 | + <li><code>0 <= nums[i] < 2<sup>10</sup></code></li> |
| 36 | +</ul> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +## Solutions |
| 42 | + |
| 43 | +**Solution: `Dynamic Programming`** |
| 44 | + |
| 45 | +- Time complexity: <em>O(n<sup>2</sup>)</em> |
| 46 | +- Space complexity: <em>O(nk)</em> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | + |
| 50 | +### **JavaScript** |
| 51 | + |
| 52 | +```js |
| 53 | +/** |
| 54 | + * @param {number[]} nums |
| 55 | + * @param {number} k |
| 56 | + * @return {number} |
| 57 | + */ |
| 58 | +const minChanges = function (nums, k) { |
| 59 | + const MAX_XOR = 1024; |
| 60 | + const n = nums.length; |
| 61 | + const counts = Array.from({ length: k }, () => new Map()); |
| 62 | + const dp = Array.from({ length: k }, () => new Array(MAX_XOR).fill(n)); |
| 63 | + |
| 64 | + const getGroupCount = index => Math.floor(n / k) + (n % k > index ? 1 : 0); |
| 65 | + |
| 66 | + for (let index = 0; index < n; index++) { |
| 67 | + const num = nums[index]; |
| 68 | + const group = counts[index % k]; |
| 69 | + const count = group.get(num) ?? 0; |
| 70 | + |
| 71 | + group.set(num, count + 1); |
| 72 | + } |
| 73 | + |
| 74 | + for (let xor = 0; xor < MAX_XOR; xor++) { |
| 75 | + const count = counts[k - 1].get(xor) ?? 0; |
| 76 | + |
| 77 | + dp[k - 1][xor] = getGroupCount(k - 1) - count; |
| 78 | + } |
| 79 | + |
| 80 | + for (let index = k - 2; index >= 0; index--) { |
| 81 | + const groupCount = getGroupCount(index); |
| 82 | + const nextMin = Math.min(...dp[index + 1]); |
| 83 | + |
| 84 | + for (let xor = 0; xor < MAX_XOR; xor++) { |
| 85 | + dp[index][xor] = groupCount + nextMin; |
| 86 | + |
| 87 | + for (const [num, count] of counts[index]) { |
| 88 | + const cost = groupCount - count; |
| 89 | + |
| 90 | + dp[index][xor] = Math.min(dp[index][xor], dp[index + 1][xor ^ num] + cost); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return dp[0][0]; |
| 96 | +}; |
| 97 | +``` |
0 commit comments