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.
Move from brute-force thinking to an efficient approach using array strategy.
nums.
A subsequence sub of nums with length x is called valid if it satisfies:
(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.Return the length of the longest valid subsequence of nums.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [1,2,3,4]
Output: 4
Explanation:
The longest valid subsequence is [1, 2, 3, 4].
Example 2:
Input: nums = [1,2,1,1,2,1,2]
Output: 6
Explanation:
The longest valid subsequence is [1, 2, 1, 2, 1, 2].
Example 3:
Input: nums = [1,3]
Output: 2
Explanation:
The longest valid subsequence is [1, 3].
Constraints:
2 <= nums.length <= 2 * 1051 <= nums[i] <= 107Problem summary: You are given an integer array nums. A subsequence sub of nums with length x is called valid if it satisfies: (sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2. Return the length of the longest valid subsequence of nums. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,4]
[1,2,1,1,2,1,2]
[1,3]
longest-increasing-subsequence)length-of-the-longest-subsequence-that-sums-to-target)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3201: Find the Maximum Length of Valid Subsequence I
class Solution {
public int maximumLength(int[] nums) {
int k = 2;
int[][] f = new int[k][k];
int ans = 0;
for (int x : nums) {
x %= k;
for (int j = 0; j < k; ++j) {
int y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[x][y]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3201: Find the Maximum Length of Valid Subsequence I
func maximumLength(nums []int) (ans int) {
k := 2
f := make([][]int, k)
for i := range f {
f[i] = make([]int, k)
}
for _, x := range nums {
x %= k
for j := 0; j < k; j++ {
y := (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
}
}
return
}
# Accepted solution for LeetCode #3201: Find the Maximum Length of Valid Subsequence I
class Solution:
def maximumLength(self, nums: List[int]) -> int:
k = 2
f = [[0] * k for _ in range(k)]
ans = 0
for x in nums:
x %= k
for j in range(k):
y = (j - x + k) % k
f[x][y] = f[y][x] + 1
ans = max(ans, f[x][y])
return ans
// Accepted solution for LeetCode #3201: Find the Maximum Length of Valid Subsequence I
impl Solution {
pub fn maximum_length(nums: Vec<i32>) -> i32 {
let mut f = [[0; 2]; 2];
let mut ans = 0;
for x in nums {
let x = (x % 2) as usize;
for j in 0..2 {
let y = ((j + 2 - x) % 2) as usize;
f[x][y] = f[y][x] + 1;
ans = ans.max(f[x][y]);
}
}
ans
}
}
// Accepted solution for LeetCode #3201: Find the Maximum Length of Valid Subsequence I
function maximumLength(nums: number[]): number {
const k = 2;
const f: number[][] = Array.from({ length: k }, () => Array(k).fill(0));
let ans: number = 0;
for (let x of nums) {
x %= k;
for (let j = 0; j < k; ++j) {
const y = (j - x + k) % k;
f[x][y] = f[y][x] + 1;
ans = Math.max(ans, f[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.