Talentd
FleetCode

3287. Find the Maximum Sequence Value of Array

Hard21.9% acceptance1 company

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

Asked at 1 company

Hints open one at a time

Hint 1
Find all the possible OR till each i with k elements backward and forward.
52:04
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxValue(self, nums: List[int], k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[2,6,7]
1
Input
[4,2,5,6,7]
2
Your code is saved on this device.Solution