You are given an integer array nums and an integer k. Your mission is to partition the array into exactly k non-empty subarrays to achieve the smallest possible maximum XOR value.
Here's how it works: For each subarray in your partition, calculate the bitwise XOR of all its elements. Among all these XOR values, find the maximum one. Your goal is to minimize this maximum XOR value across all possible ways to partition the array.
Example: If nums = [1, 2, 3, 4] and k = 2, you could partition it as [1, 2] | [3, 4]. The XOR values would be 1⊕2 = 3 and 3⊕4 = 7, so the maximum XOR is 7. But is this optimal?
This is a classic minimize the maximum problem that combines dynamic programming with binary search techniques!
Input & Output
Constraints
- 1 ≤ k ≤ nums.length ≤ 15
- 1 ≤ nums[i] ≤ 104
- Each subarray must be non-empty
- Array elements are positive integers