Find the Maximum Sequence Value of Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,6,7], k = 1
Output: 5
💡 Note: Choose subsequence [2,7]. First half: OR(2) = 2. Second half: OR(7) = 7. XOR: 2 ^ 7 = 5
Example 2 — Larger k
$ Input: nums = [4,2,5,6,7], k = 2
Output: 2
💡 Note: Choose subsequence [4,5,6,7]. First half: OR(4,5) = 5. Second half: OR(6,7) = 7. XOR: 5 ^ 7 = 2
Example 3 — All Same Values
$ Input: nums = [3,3,3,3], k = 2
Output: 0
💡 Note: Any subsequence gives OR(3,3) = 3 for both halves. XOR: 3 ^ 3 = 0

Constraints

  • 1 ≤ k ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 26
  • nums.length is even
  • 2 * k ≤ nums.length

Visualization

Tap to expand
Maximum Sequence Value: Split → OR → XOR26153Choose subsequence of size 2k = 4Group 1: [2,1]Group 2: [6,5]OR(2,1) = 3OR(6,5) = 73 XOR 7 = 4
Understanding the Visualization
1
Input
Array nums and parameter k
2
Choose Subsequence
Pick 2k elements and split into two groups of k
3
Compute Value
OR each group, then XOR the results
Key Takeaway
🎯 Key Insight: Use DP to build all possible OR values efficiently, then find maximum XOR
Asked in
Google 15 Facebook 12 Microsoft 8
12.0K Views
Medium Frequency
~35 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen