Problems > Longest Arithmetic Subsequence

Longest Arithmetic Subsequence

Difficulty: 1800 5000 ms 128 MB

dp

Print the length of the longest subsequence (not necessarily contiguous) forming an arithmetic progression.

Input: the first line has N; the second line has N integers.

Output: the longest arithmetic subsequence length.

Constraints:

  • 1 ≤ N ≤ 1000
  • |ai| ≤ 109

Sample Tests

Input 1
4
3 6 9 12
Output 1
4
Input 2
3
1 2 4
Output 2
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.