You are given an integer array nums and a positive integer k.
The value of a sequence seq of size 2 * x is defined as:
(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1]).Return the maximum value of any subsequence of nums having size 2 * k.
Example 1:
Input: nums = [2,6,7], k = 1
Output: 5
Explanation:
The subsequence [2, 7] has the maximum value of 2 XOR 7 = 5.
Example 2:
Input: nums = [4,2,5,6,7], k = 2
Output: 2
Explanation:
The subsequence [4, 5, 6, 7] has the maximum value of (4 OR 5) XOR (6 OR 7) = 2.
Constraints:
2 <= nums.length <= 4001 <= nums[i] < 271 <= k <= nums.length / 2Problem Overview: You receive an integer array and must construct a sequence that maximizes a specific value derived from bitwise operations across selected elements. The challenge is choosing elements in the right order or partition so the resulting bitwise combination produces the largest possible value.
Approach 1: Sorting and Partitioning (Time: O(n log n), Space: O(1) or O(n))
This strategy sorts the array to prioritize numbers with higher bit contribution. After sorting, you partition the array into candidate segments and evaluate the sequence value produced by combining elements from each partition. Sorting helps cluster numbers with strong high‑bit influence, which tends to increase the resulting bitwise expression. The algorithm iterates through valid split points and calculates the resulting sequence value using efficient bitwise OR or XOR operations. This method works well when the optimal sequence is dominated by elements containing larger high bits.
The core idea relies on understanding how bit patterns interact. Numbers with overlapping high bits amplify the result when combined, so grouping them strategically increases the sequence value. While sorting adds an O(n log n) cost, the subsequent evaluation step is linear.
Approach 2: Greedy Selection with Sliding Window (Time: O(n), Space: O(1))
A more efficient method scans the array while dynamically maintaining a candidate sequence. Using a sliding window, you track the current set of elements contributing to the sequence value and update the running bitwise state as elements enter or leave the window. The greedy insight is that if a new element improves the bitwise outcome, it should be included while less useful elements can be discarded.
The window maintains the best possible sequence under the current constraints, updating the computed value using constant‑time bit operations. Because each element is processed at most twice (entering and leaving the window), the total runtime remains linear. This technique is common in problems mixing array traversal with incremental state updates.
The algorithm benefits from properties of bit manipulation. Bitwise operations are associative and cheap to compute, allowing the running value to be updated efficiently without recomputing the entire sequence each time.
Recommended for interviews: Interviewers typically expect the optimized greedy or dynamic approach because it shows you understand how bitwise contributions accumulate and how to maintain the optimal candidate while scanning the array. A sorting‑based method demonstrates the core intuition, but the linear sliding window solution better reflects mastery of dynamic programming style state tracking and greedy optimization.
In this approach, you first sort the array and select the top k largest numbers. By partitioning these numbers, you can find the 'OR' and 'XOR' values by maximizing the 'OR' within each half.
The idea is to use the sorted property to our advantage by considering high-value elements together to optimize the partition between the two halves.
We sort the array in descending order to prioritize large numbers. We then partition the sorted array into two halves and perform 'OR' operations within each half. Finally, we XOR the two halves to ascertain the sequence's value and return it.
The time complexity is dominated by the sorting step, making it O(n log n), where n is the length of the array. The space complexity is O(1) as we perform operations in-place.
This approach involves utilizing slide windows to select a subarray of size 2*k, picking the largest elements from either end and determining the break point to maximize the OR-XOR sequence value.
The trick lies in the greedy selection: ensuring we extract chunks of advantageous 'OR' value pairs.
The C program uses a sliding window to check each subarray partition as potential maximum OR-XOR sequence and updates the running maximum. It leverages direct OR operations and XOR between two generated halves in linearly traversed segments.
Time complexity is O(n*k), being a sliding window approach. Space complexity remains O(1), as swapping is done in-place.
We consider dividing the sequence into two parts, the first k elements and the last k elements, and calculate all possible XOR values for the prefixes and suffixes.
Define f[i][j][x] to represent whether there exists a subset with an XOR value of x by taking j elements from the first i elements. Define g[i][j][y] to represent whether there exists a subset with an XOR value of y by taking j elements starting from index i.
Consider the transition equation for f[i][j][x]. For the i-th element (starting from 0), we can choose to take it or not, so we have:
$
f[i + 1][j][x] = f[i + 1][j][x] \lor f[i][j][x] \
f[i + 1][j + 1][x \lor nums[i]] = f[i + 1][j + 1][x \lor nums[i]] \lor f[i][j][x]
For the transition equation of g[i][j][y], similarly for the i-th element (starting from n - 1), we can choose to take it or not, so we have:
g[i - 1][j][y] = g[i - 1][j][y] \lor g[i][j][y] \
g[i - 1][j + 1][y \lor nums[i - 1]] = g[i - 1][j + 1][y \lor nums[i - 1]] \lor g[i][j][y]
Finally, we enumerate i in the range [k, n - k]. For each i, we enumerate x and y, where 0 leq x, y < 2^7. If both f[i][k][x] and g[i][k][y] are true, we update the answer ans = max(ans, x \oplus y).
The time complexity is O(n times m times k), and the space complexity is O(n times m times k), where n is the length of the array, and m = 2^7$.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Sorting and Partitioning | The time complexity is dominated by the sorting step, making it O(n log n), where n is the length of the array. The space complexity is O(1) as we perform operations in-place. |
| Approach 2: Greedy Selection with Sliding Window | Time complexity is O(n*k), being a sliding window approach. Space complexity remains O(1), as swapping is done in-place. |
| Dynamic Programming + Prefix and Suffix Decomposition + Enumeration | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting and Partitioning | O(n log n) | O(1) – O(n) | Good baseline approach when evaluating candidate splits after ordering elements by bit contribution. |
| Greedy Selection with Sliding Window | O(n) | O(1) | Best for large arrays where maintaining a running bitwise value while scanning yields the optimal sequence. |
3287. Find the Maximum Sequence Value of Array | Leetcode Biweekly 139 • codingMohan • 2,114 views views
Watch 3 more video solutions →Practice Find the Maximum Sequence Value of Array with our built-in code editor and test cases.
Practice on FleetCode