Partition Array to Minimize XOR

You are given an integer array nums and an integer k.

Your task is to partition nums into k non-empty subarrays. For each subarray, compute the bitwise XOR of all its elements.

Return the minimum possible value of the maximum XOR among these k subarrays.

Example 1
Inputnums = [1,2,3], k = 2
Output1
The optimal partition is [1] and [2, 3], whose XOR values are both 1, so the maximum XOR is minimized to 1.
Example 2
Inputnums = [2,3,3,2], k = 3
Output2
The optimal partition is [2], [3, 3], and [2], whose XOR values are 2, 0, and 2, so the maximum XOR is minimized to 2.

Constraints

  • 1 <= nums.length <= 250
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= n

Asked at 1 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