-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongestAP.cpp
More file actions
54 lines (45 loc) · 824 Bytes
/
longestAP.cpp
File metadata and controls
54 lines (45 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Finds length of longest AP possible in an array
// Input = {1, 7, 10, 15, 27, 29}
// Output = 3
// The longest arithmetic progression is {1, 15, 29}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dbl;
#define fr(a,b) for(ll i=a;i<b;i++)
int main(){
ll n;
cin>>n;
ll a[n];
fr(0,n) cin>>a[i];
sort(a,a+n);
ll dp[n][n];
ll i,j,k;
fr(0,n) for(j=0;j<n;j++) dp[i][j]=2;
if(n<=2){
cout<<n;
return 0;
}
ll len=2;
for(j=n-1;j>=1;j--){
i=j-1;
k=j+1;
while(i>=0 && k<=n-1){
if(a[i]+a[k]>(2*a[j])){
dp[i][j]=2;
i--;
}
else if(a[i]+a[k]<(2*a[j])){
k++;
}
else{
dp[i][j]=dp[j][k]+1;
len=max(len,dp[i][j]);
i--;
k++;
}
}
}
cout<<len;
return 0;
}