DSA Tracker

Medium

Longest Arithmetic Subsequence

A medium Dynamic Programming problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Dynamic Programming
Sheets
1
Core for
9 roles
Platform
LeetCode

The problem

Given an integer array, find the length of the longest arithmetic subsequence. An arithmetic subsequence has a constant difference between consecutive elements.

Example 1

Input
nums = [3,6,9,12]
Output
4
Why
The entire array is an arithmetic sequence with common difference 3, length 4.

Example 2

Input
nums = [9,4,7,2,10]
Output
3
Why
The longest arithmetic subsequence is [4,7,10] with common difference 3.

Example 3

Input
nums = [20,1,15,3,10,5,8]
Output
4
Why
The longest arithmetic subsequence is [20,15,10,5] with common difference -5.

Constraints

  • 2 <= nums.length <= 1000
  • 0 <= nums[i] <= 500

How to think about it

Updated 2026-09-09

An arithmetic subsequence is governed by two parameters at its tip: the ending index and the common difference d. At each index i, pairing with any earlier index j gives difference d = nums[i] - nums[j], extending the longest sequence ending at j with that same d by 1.

Approaches, worst first

  1. Exhaustive subsequence checking

    time O(2^n) · space O(n)

    Generate all subsequences of length >= 2, check if differences are equal, and track max length. Combinatorial explosion of 2^n subsequences.

  2. Array of hash maps

    time O(n^2) · space O(n^2)

    dp[i] is a hash map mapping difference d to the longest sequence length ending at i. For each i, scan j < i: d = nums[i] - nums[j], dp[i][d] = dp[j].get(d, 1) + 1.

  3. Offset 2D lookup tableWrite this one

    time O(n^2) · space O(n * max_diff)

    Because values are bounded between 0 and 500, differences d range from -500 to 500. Shift differences by offset +500 to index into a static 2D array dp[n][1001], avoiding hash map allocation overhead.

Where people lose marks · 3
  • Negative differences: nums[i] - nums[j] can be negative, which requires careful offset shifting or default map handling.
  • Default length for a new pair: any pair of two numbers automatically forms an arithmetic sequence of length 2.
  • Overwriting longer sequences with shorter duplicate differences: take max instead of plain assignment.

The theory behind it

Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems

What Dynamic Programming is

Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.

When to reach for it

Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.

How the pattern works

Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.

What each operation costs

OperationTime
fill dynamic programming table of n statesO(n)
solve two-dimensional grid of m by n statesO(m * n)
space-optimized state transition keeping one rowO(n)
What usually goes wrong with Dynamic Programming
  • Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
  • Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
  • Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.

Which roles need this problem

Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Dynamic Programming problems

Problem set and role mapping as of .