You are given an integer array nums and a positive integer k. Your goal is to find the maximum possible value of a specific type of subsequence.
The value of a sequence seq of size 2 * k is calculated as follows:
- Split the sequence into two equal halves of size
keach - Calculate the OR of all elements in the first half
- Calculate the OR of all elements in the second half
- Return the XOR of these two OR results
Formally: (seq[0] OR seq[1] OR ... OR seq[k-1]) XOR (seq[k] OR seq[k+1] OR ... OR seq[2*k-1])
Your task is to find any subsequence of nums with exactly 2 * k elements that produces the maximum value according to this formula.
Example: If nums = [2,6,7] and k = 1, we need a subsequence of size 2. The subsequence [6,7] gives us 6 XOR 7 = 1, while [2,7] gives us 2 XOR 7 = 5.
Input & Output
Visualization
Time & Space Complexity
For each of n positions and k group sizes, we may have up to 2^20 different OR values (since integers are typically 32-bit, but practical OR combinations are limited)
DP table storing sets of OR values for each state
Constraints
- 1 โค nums.length โค 50
- 0 โค k โค nums.length // 2
- 1 โค nums[i] โค 106
- The array must have at least 2*k elements
- All elements are positive integers