Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

What is the length of the longest arithmetic subsequence (equal consecutive differences, chosen by index) of [3, 6, 9, 12, 5, 10, 15, 20, 1]?

  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

dp[i][d] = longest arithmetic subsequence ending at index i with common difference d, built from every earlier j. The difference is a key, so the dictionary per index makes the transitions O(n²).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic