Skip to content
Veltoryx Technologies
ProProblem 147● Mediumleetcode ↗3 min readUpdated 30 October 2026

Longest Arithmetic Subsequence

Find the longest arithmetic progression subsequence by caching sequence lengths indexed by common difference.

01The problem

Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.

Recall that a subsequence of an array is a list nums[i1], nums[i2], ..., nums[ik] with 0 <= i1 < i2 < ... < ik <= nums.length - 1, and that sequence is arithmetic if nums[i(t+1)] - nums[it] is the same value for every 1 <= t < k.

Loading the rest of the lesson…