Longest Arithmetic Progression
InterviewTime limit2sMemory limit1024 MB
Find the length of the longest subsequence of the given sorted list that forms an arithmetic progression.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
An arithmetic progression is an ascending sequence in which the difference between two consecutive elements is always the same. For example, is an arithmetic progression.
A subsequence of an ascending sequence of numbers is an ascending sequence with whose elements all occur in . For example, , , and are subsequences of .
You are given an ascending sequence . Find the length of a longest arithmetic progression that is a subsequence of . There may be several longest arithmetic progressions, but the length is unique.
A sequence of one element or two elements also satisfies the definition, so the answer is never smaller than 2.
is at least 10 and at most 500, and every element of is a positive integer smaller than 100000.
Input
The input has two lines. The first line contains the number of elements of . The second line contains the elements of in ascending order, separated by spaces.
Output
Print, on one line, the length of the longest arithmetic progression that is a subsequence of .