Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[2,6,7] 1
[4,2,5,6,7] 2
bitwise-ors-of-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3287: Find the Maximum Sequence Value of Array
class Solution {
public int maxValue(int[] nums, int k) {
int m = 1 << 7;
int n = nums.length;
boolean[][][] f = new boolean[n + 1][k + 2][m];
f[0][0][0] = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
for (int x = 0; x < m; x++) {
if (f[i][j][x]) {
f[i + 1][j][x] = true;
f[i + 1][j + 1][x | nums[i]] = true;
}
}
}
}
boolean[][][] g = new boolean[n + 1][k + 2][m];
g[n][0][0] = true;
for (int i = n; i > 0; i--) {
for (int j = 0; j <= k; j++) {
for (int y = 0; y < m; y++) {
if (g[i][j][y]) {
g[i - 1][j][y] = true;
g[i - 1][j + 1][y | nums[i - 1]] = true;
}
}
}
}
int ans = 0;
for (int i = k; i <= n - k; i++) {
for (int x = 0; x < m; x++) {
if (f[i][k][x]) {
for (int y = 0; y < m; y++) {
if (g[i][k][y]) {
ans = Math.max(ans, x ^ y);
}
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3287: Find the Maximum Sequence Value of Array
func maxValue(nums []int, k int) int {
m := 1 << 7
n := len(nums)
f := make([][][]bool, n+1)
for i := range f {
f[i] = make([][]bool, k+2)
for j := range f[i] {
f[i][j] = make([]bool, m)
}
}
f[0][0][0] = true
for i := 0; i < n; i++ {
for j := 0; j <= k; j++ {
for x := 0; x < m; x++ {
if f[i][j][x] {
f[i+1][j][x] = true
f[i+1][j+1][x|nums[i]] = true
}
}
}
}
g := make([][][]bool, n+1)
for i := range g {
g[i] = make([][]bool, k+2)
for j := range g[i] {
g[i][j] = make([]bool, m)
}
}
g[n][0][0] = true
for i := n; i > 0; i-- {
for j := 0; j <= k; j++ {
for y := 0; y < m; y++ {
if g[i][j][y] {
g[i-1][j][y] = true
g[i-1][j+1][y|nums[i-1]] = true
}
}
}
}
ans := 0
for i := k; i <= n-k; i++ {
for x := 0; x < m; x++ {
if f[i][k][x] {
for y := 0; y < m; y++ {
if g[i][k][y] {
ans = max(ans, x^y)
}
}
}
}
}
return ans
}
# Accepted solution for LeetCode #3287: Find the Maximum Sequence Value of Array
class Solution:
def maxValue(self, nums: List[int], k: int) -> int:
m = 1 << 7
n = len(nums)
f = [[[False] * m for _ in range(k + 2)] for _ in range(n + 1)]
f[0][0][0] = True
for i in range(n):
for j in range(k + 1):
for x in range(m):
f[i + 1][j][x] |= f[i][j][x]
f[i + 1][j + 1][x | nums[i]] |= f[i][j][x]
g = [[[False] * m for _ in range(k + 2)] for _ in range(n + 1)]
g[n][0][0] = True
for i in range(n, 0, -1):
for j in range(k + 1):
for y in range(m):
g[i - 1][j][y] |= g[i][j][y]
g[i - 1][j + 1][y | nums[i - 1]] |= g[i][j][y]
ans = 0
for i in range(k, n - k + 1):
for x in range(m):
if f[i][k][x]:
for y in range(m):
if g[i][k][y]:
ans = max(ans, x ^ y)
return ans
// Accepted solution for LeetCode #3287: Find the Maximum Sequence Value of Array
/**
* [3287] Find the Maximum Sequence Value of Array
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashSet;
impl Solution {
pub fn max_value(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let length = nums.len();
let search = |array: &Vec<i32>| {
let mut dp = Vec::with_capacity(array.len());
let mut prev = vec![HashSet::new(); k + 1];
prev[0].insert(0);
for i in 0..array.len() {
for j in (0..=(i + 1).min(k - 1)).rev() {
// 使用split_at_mut以通过借用检查
let (first, second) = prev.split_at_mut(j + 1);
for x in first[j].iter().map(|x| *x | array[i]) {
second[0].insert(x);
}
}
dp.push(prev[k].clone());
}
dp
};
let a = search(&nums);
let reversed_nums: Vec<i32> = nums.iter().rev().map(|x| x.clone()).collect();
let b = search(&reversed_nums);
let mut result = 0;
for i in k - 1..length - k {
for &a_val in a[i].iter() {
for &b_val in b[length - i - 2].iter() {
result = result.max(a_val ^ b_val);
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3287() {
assert_eq!(5, Solution::max_value(vec![2, 6, 7], 1));
assert_eq!(2, Solution::max_value(vec![4, 2, 5, 6, 7], 2));
}
}
// Accepted solution for LeetCode #3287: Find the Maximum Sequence Value of Array
function maxValue(nums: number[], k: number): number {
const m = 1 << 7;
const n = nums.length;
const f: boolean[][][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: k + 2 }, () => Array(m).fill(false)),
);
f[0][0][0] = true;
for (let i = 0; i < n; i++) {
for (let j = 0; j <= k; j++) {
for (let x = 0; x < m; x++) {
if (f[i][j][x]) {
f[i + 1][j][x] = true;
f[i + 1][j + 1][x | nums[i]] = true;
}
}
}
}
const g: boolean[][][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: k + 2 }, () => Array(m).fill(false)),
);
g[n][0][0] = true;
for (let i = n; i > 0; i--) {
for (let j = 0; j <= k; j++) {
for (let y = 0; y < m; y++) {
if (g[i][j][y]) {
g[i - 1][j][y] = true;
g[i - 1][j + 1][y | nums[i - 1]] = true;
}
}
}
}
let ans = 0;
for (let i = k; i <= n - k; i++) {
for (let x = 0; x < m; x++) {
if (f[i][k][x]) {
for (let y = 0; y < m; y++) {
if (g[i][k][y]) {
ans = Math.max(ans, x ^ y);
}
}
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.