Skip to content

Commit 0d7c4bb

Browse files
authored
Added task 1787.
1 parent 4a0ec22 commit 0d7c4bb

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1701_1800.s1787_make_the_xor_of_all_segments_equal_to_zero;
2+
3+
// #Hard #Array #Dynamic_Programming #Bit_Manipulation
4+
// #2022_04_27_Time_102_ms_(89.47%)_Space_77.4_MB_(73.68%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int minChanges(int[] nums, int k) {
10+
int n = nums.length;
11+
int[][] fre = new int[k][1024];
12+
for (int i = 0; i < n; i++) {
13+
fre[i % k][nums[i]]++;
14+
}
15+
int[] dp = new int[1024];
16+
Arrays.fill(dp, -n);
17+
dp[0] = 0;
18+
int max = 0;
19+
for (int i = 0; i < k; i++) {
20+
int[] dp2 = new int[1024];
21+
Arrays.fill(dp2, max);
22+
int max2 = 0;
23+
for (int xor = 0; xor < 1024; xor++) {
24+
for (int al = i; al < n; al += k) {
25+
dp2[xor] = Math.max(dp2[xor], dp[xor ^ nums[al]] + fre[i][nums[al]]);
26+
}
27+
max2 = Math.max(max2, dp2[xor]);
28+
}
29+
max = max2;
30+
dp = dp2;
31+
}
32+
return n - dp[0];
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1787\. Make the XOR of All Segments Equal to Zero
2+
3+
Hard
4+
5+
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]`.
6+
7+
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.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,0,3,0], k = 1
12+
13+
**Output:** 3
14+
15+
**Explanation:** Modify the array from [**1**,**2**,0,**3**,0] to from [**0**,**0**,0,**0**,0].
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,4,5,2,1,7,3,4,7], k = 3
20+
21+
**Output:** 3
22+
23+
**Explanation:** Modify the array from [3,4,**5**,**2**,**1**,7,3,4,7] to [3,4,**7**,**3**,**4**,7,3,4,7].
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1,2,4,1,2,5,1,2,6], k = 3
28+
29+
**Output:** 3
30+
31+
**Explanation:** Modify the array from [1,2,**4,**1,2,**5**,1,2,**6**] to [1,2,**3**,1,2,**3**,1,2,**3**].
32+
33+
**Constraints:**
34+
35+
* `1 <= k <= nums.length <= 2000`
36+
* <code>0 <= nums[i] < 2<sup>10</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1701_1800.s1787_make_the_xor_of_all_segments_equal_to_zero;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minChanges() {
11+
assertThat(new Solution().minChanges(new int[] {1, 2, 0, 3, 0}, 1), equalTo(3));
12+
}
13+
14+
@Test
15+
void minChanges2() {
16+
assertThat(new Solution().minChanges(new int[] {3, 4, 5, 2, 1, 7, 3, 4, 7}, 3), equalTo(3));
17+
}
18+
19+
@Test
20+
void minChanges3() {
21+
assertThat(new Solution().minChanges(new int[] {1, 2, 4, 1, 2, 5, 1, 2, 6}, 3), equalTo(3));
22+
}
23+
}

0 commit comments

Comments
 (0)