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 and a positive integer k.
A subsequence sub of nums with length x is called valid if it satisfies:
(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.nums.
Example 1:
Input: nums = [1,2,3,4,5], k = 2
Output: 5
Explanation:
The longest valid subsequence is [1, 2, 3, 4, 5].
Example 2:
Input: nums = [1,4,2,3,1,4], k = 3
Output: 4
Explanation:
The longest valid subsequence is [1, 4, 1, 4].
Constraints:
2 <= nums.length <= 1031 <= nums[i] <= 1071 <= k <= 103Problem summary: You are given an integer array nums and a positive integer k. A subsequence sub of nums with length x is called valid if it satisfies: (sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k. Return the length of the longest valid subsequence of nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,4,5] 2
[1,4,2,3,1,4] 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 #3202: Find the Maximum Length of Valid Subsequence II
class Solution {
public int maximumLength(int[] nums, int k) {
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 #3202: Find the Maximum Length of Valid Subsequence II
func maximumLength(nums []int, k int) (ans int) {
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 #3202: Find the Maximum Length of Valid Subsequence II
class Solution:
def maximumLength(self, nums: List[int], k: int) -> int:
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 #3202: Find the Maximum Length of Valid Subsequence II
impl Solution {
pub fn maximum_length(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let mut f = vec![vec![0; k]; k];
let mut ans = 0;
for x in nums {
let x = (x % k as i32) as usize;
for j in 0..k {
let y = (j + k - x) % k;
f[x][y] = f[y][x] + 1;
ans = ans.max(f[x][y]);
}
}
ans
}
}
// Accepted solution for LeetCode #3202: Find the Maximum Length of Valid Subsequence II
function maximumLength(nums: number[], k: number): number {
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.