Skip to main content

Find the Maximum Sequence Value of Array - Solution & Explanation

HardArrayDynamic ProgrammingBit Manipulation22 min readAsked at: Google
Practice this problem

Problem Statement

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 <= 400
  • 1 <= nums[i] < 27
  • 1 <= k <= nums.length / 2

Approach Overview

Problem 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.

Approach 1: Approach 1: Sorting and Partitioning

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Approach 2: Greedy Selection with Sliding Window

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity is O(n*k), being a sliding window approach. Space complexity remains O(1), as swapping is done in-place.

Try this approach in the editor →

Approach 3: Dynamic Programming + Prefix and Suffix Decomposition + Enumeration

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$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting and PartitioningO(n log n)O(1) – O(n)Good baseline approach when evaluating candidate splits after ordering elements by bit contribution.
Greedy Selection with Sliding WindowO(n)O(1)Best for large arrays where maintaining a running bitwise value while scanning yields the optimal sequence.

Video Solution

3287. Find the Maximum Sequence Value of Array | Leetcode Biweekly 139 • codingMohan • 2,245 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Find the Maximum Sequence Value of Array easy or hard?
Find the Maximum Sequence Value of Array is classified as a Hard problem. The difficulty comes from understanding how bitwise contributions interact across elements and designing an efficient greedy or dynamic approach rather than brute-force enumeration.
Find the Maximum Sequence Value of Array Python/Java solution
The algorithm translates directly across languages. Python, Java, C++, and JavaScript implementations use the same core idea: iterate through the array, update the running bitwise value with OR/XOR operations, and maintain the best sequence through greedy or partition-based logic.
How to solve Find the Maximum Sequence Value of Array in O(n)?
Use a sliding window that maintains the current sequence and its running bitwise value. As you iterate through the array, include elements that improve the sequence value and remove elements that reduce the optimal result. Each update relies on constant‑time bitwise operations, keeping the overall runtime linear.
What is the best approach for Find the Maximum Sequence Value of Array?
The most efficient solution uses a greedy strategy combined with a sliding window to maintain the best sequence while scanning the array. By updating the running bitwise value incrementally, the algorithm processes each element at most twice and achieves O(n) time complexity with constant extra space.
Is Find the Maximum Sequence Value of Array asked at Google/Amazon/Meta?
Hard array and bit manipulation problems similar to this frequently appear in interviews at large tech companies including Google, Amazon, and Meta. These companies test the ability to combine greedy reasoning, dynamic state tracking, and efficient bit operations.
What data structure is used in Find the Maximum Sequence Value of Array?
The problem mainly relies on arrays and bitwise operations. Optimized solutions maintain a sliding window or dynamic state representing the current sequence value, avoiding heavy auxiliary data structures.
What is the time complexity of Find the Maximum Sequence Value of Array?
The optimal approach runs in O(n) time using a greedy sliding window technique. A simpler alternative sorts the array and evaluates candidate partitions, which takes O(n log n) time due to the sorting step. Space complexity is typically O(1) aside from the input array.

Ready to solve this problem?

Practice Find the Maximum Sequence Value of Array with our built-in code editor and test cases.

Practice on FleetCode