This page is still under construction.

Parts of this page are still being built. What you see may change.

Longest Arithmetic Progression

Interview

Time limit2sMemory limit1024 MB

Summary
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 a1<a2<⋯<ana_1 < a_2 < \dots < a_n in which the difference between two consecutive elements is always the same. For example, 11<21<31<41<5111 < 21 < 31 < 41 < 51 is an arithmetic progression.

A subsequence of an ascending sequence aa of nn numbers is an ascending sequence b1<b2<⋯<bmb_1 < b_2 < \dots < b_m with m≤nm \le n whose elements all occur in aa. For example, 21<41<5121 < 41 < 51, 11<4111 < 41, and 11<21<31<41<5111 < 21 < 31 < 41 < 51 are subsequences of 11<21<31<41<5111 < 21 < 31 < 41 < 51.

You are given an ascending sequence c1<c2<⋯<ckc_1 < c_2 < \dots < c_k. Find the length of a longest arithmetic progression that is a subsequence of cc. 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.

kk is at least 10 and at most 500, and every element of cc is a positive integer smaller than 100000.

Input

The input has two lines. The first line contains the number of elements kk of cc. The second line contains the elements of cc in ascending order, separated by spaces.

Output

Print, on one line, the length of the longest arithmetic progression that is a subsequence of cc.

Examples1

  1. Example 1

    Input
    12
    1 2 4 5 7 8 9 11 13 14 15 16
    
    Expected output
    6