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.
Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
Note that:
seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).Example 1:
Input: nums = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3.
Example 2:
Input: nums = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10].
Example 3:
Input: nums = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].
Constraints:
2 <= nums.length <= 10000 <= nums[i] <= 500Problem summary: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums. Note that: 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. A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Binary Search · Dynamic Programming
[3,6,9,12]
[9,4,7,2,10]
[20,1,15,3,10,5,8]
destroy-sequential-targets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1027: Longest Arithmetic Subsequence
class Solution {
public int longestArithSeqLength(int[] nums) {
int n = nums.length;
int ans = 0;
int[][] f = new int[n][1001];
for (int i = 1; i < n; ++i) {
for (int k = 0; k < i; ++k) {
int j = nums[i] - nums[k] + 500;
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
ans = Math.max(ans, f[i][j]);
}
}
return ans + 1;
}
}
// Accepted solution for LeetCode #1027: Longest Arithmetic Subsequence
func longestArithSeqLength(nums []int) int {
n := len(nums)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, 1001)
}
ans := 0
for i := 1; i < n; i++ {
for k := 0; k < i; k++ {
j := nums[i] - nums[k] + 500
f[i][j] = max(f[i][j], f[k][j]+1)
ans = max(ans, f[i][j])
}
}
return ans + 1
}
# Accepted solution for LeetCode #1027: Longest Arithmetic Subsequence
class Solution:
def longestArithSeqLength(self, nums: List[int]) -> int:
n = len(nums)
f = [[1] * 1001 for _ in range(n)]
ans = 0
for i in range(1, n):
for k in range(i):
j = nums[i] - nums[k] + 500
f[i][j] = max(f[i][j], f[k][j] + 1)
ans = max(ans, f[i][j])
return ans
// Accepted solution for LeetCode #1027: Longest Arithmetic Subsequence
struct Solution;
use std::collections::HashMap;
impl Solution {
fn longest_arith_seq_length(a: Vec<i32>) -> i32 {
let mut res = 0;
let n = a.len();
let mut dp: Vec<HashMap<i32, i32>> = vec![HashMap::new(); n];
for i in 0..n {
for j in 0..i {
let diff = a[i] - a[j];
let len_j = *dp[j].entry(diff).or_default();
let len_i = dp[i].entry(diff).or_default();
*len_i = len_j + 1;
res = res.max(*len_i);
}
}
res + 1
}
}
#[test]
fn test() {
let a = vec![3, 6, 9, 12];
let res = 4;
assert_eq!(Solution::longest_arith_seq_length(a), res);
let a = vec![9, 4, 7, 2, 10];
let res = 3;
assert_eq!(Solution::longest_arith_seq_length(a), res);
let a = vec![20, 1, 15, 3, 10, 5, 8];
let res = 4;
assert_eq!(Solution::longest_arith_seq_length(a), res);
}
// Accepted solution for LeetCode #1027: Longest Arithmetic Subsequence
function longestArithSeqLength(nums: number[]): number {
const n = nums.length;
let ans = 0;
const f: number[][] = Array.from({ length: n }, () => new Array(1001).fill(0));
for (let i = 1; i < n; ++i) {
for (let k = 0; k < i; ++k) {
const j = nums[i] - nums[k] + 500;
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
ans = Math.max(ans, f[i][j]);
}
}
return ans + 1;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
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.