Arithmetic Progressions
InterviewTime limit5sMemory limit512 MB
Given up to 5000 distinct numbers, find the length of the longest subset that forms an arithmetic progression.
- Level
Medium6 of 10
- Topics
- Array, Hash map, Dynamic programming
- Solved
- No attempts yet
Problem
An arithmetic progression is a sequence in which the difference of consecutive terms is constant (). For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with common difference 3.
In this problem, you must find the length of the longest arithmetic progression that can be formed by selecting some numbers from a given set of numbers. For example, if the given set is , you can form progressions such as 0, 3, 6, 9 with common difference 3, or 9, 5, 1 with common difference . Here 0, 3, 6, 9 and 9, 6, 3, 0 are the longest.
Input
The input consists of a single test case in the following format.
n
v1 v2 ··· vn
is the number of elements in the set, an integer satisfying . Each () is an element of the set, an integer satisfying . All are distinct, that is, if .
Output
Output the length of the longest arithmetic progression that can be formed by selecting some numbers from the given set.