3599. Partition Array to Minimize XOR Medium
1/**
2 * [3599] Partition Array to Minimize XOR
3 *
4 * You are given an integer array nums and an integer k.
5 * Your task is to partition nums into k non-empty <span data-keyword="subarray-nonempty">subarrays</span>. For each subarray, compute the bitwise XOR of all its elements.
6 * Return the minimum possible value of the maximum XOR among these k subarrays.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [1,2,3], k = 2</span>
11 * Output: <span class="example-io">1</span>
12 * Explanation:
13 * The optimal partition is [1] and [2, 3].
14 *
15 * XOR of the first subarray is 1.
16 * XOR of the second subarray is 2 XOR 3 = 1.
17 *
18 * The maximum XOR among the subarrays is 1, which is the minimum possible.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">nums = [2,3,3,2], k = 3</span>
23 * Output: <span class="example-io">2</span>
24 * Explanation:
25 * The optimal partition is [2], [3, 3], and [2].
26 *
27 * XOR of the first subarray is 2.
28 * XOR of the second subarray is 3 XOR 3 = 0.
29 * XOR of the third subarray is 2.
30 *
31 * The maximum XOR among the subarrays is 2, which is the minimum possible.
32 * </div>
33 * <strong class="example">Example 3:
34 * <div class="example-block">
35 * Input: <span class="example-io">nums = [1,1,2,3,1], k = 2</span>
36 * Output: <span class="example-io">0</span>
37 * Explanation:
38 * The optimal partition is [1, 1] and [2, 3, 1].
39 *
40 * XOR of the first subarray is 1 XOR 1 = 0.
41 * XOR of the second subarray is 2 XOR 3 XOR 1 = 0.
42 *
43 * The maximum XOR among the subarrays is 0, which is the minimum possible.
44 * </div>
45 *
46 * Constraints:
47 *
48 * 1 <= nums.length <= 250
49 * 1 <= nums[i] <= 10^9
50 * 1 <= k <= n
51 *
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/partition-array-to-minimize-xor/
56// discuss: https://leetcode.com/problems/partition-array-to-minimize-xor/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61 pub fn min_xor(nums: Vec<i32>, k: i32) -> i32 {
62 0
63 }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_3599() {
74 }
75}
76Back
© 2026 bowen.ge All Rights Reserved.