-
Notifications
You must be signed in to change notification settings - Fork 8
/
longest-ap.cpp
executable file
·69 lines (68 loc) · 1.99 KB
/
longest-ap.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
#include <bits/stdc++.h>
using namespace std;
int LongestAP(int arr[], int n){
unordered_map <int, vector<pair<int,int> > > mp;
int lengthStore[1000];
for(int i=0; i<n;i++){
for(int j=i+1;j<n;j++){
int diff = arr[j] - arr[i];
if(mp.find(diff) == mp.end()){
vector<pair<int,int> > temp;
temp.push_back(make_pair(i,j));
mp.insert(make_pair(diff,temp));
temp.clear();
}else{
// key already present
vector<pair<int,int> > temp(mp[diff]);
temp.push_back(make_pair(i,j));
mp[diff] = temp;
temp.clear();
}
}
}
//initialize lengthStore arr with value = 1
for(int i=0;i<n;i++){
lengthStore[i] = 1;
}
unordered_map <int, vector<pair<int,int> > > :: iterator it;
// for(it = mp.begin();it!=mp.end();it++){
// cout<<it->first<<endl;
// vector<pair<int,int> > temp(it->second);
// for(int i=0;i<temp.size();i++){
// cout<<"("<<temp[i].first<<", "<<temp[i].second<<"),";
// //lengthStore[temp[i].second] = lengthStore[temp[i].first] + 1;
// }
// cout<<endl;
// }
for(it = mp.begin(); it!=mp.end(); it++){
vector<pair<int,int> > temp(it->second);
for(int i=0;i<temp.size();i++){
//cout<<lengthStore[temp[i].first]<<" "<<lengthStore[temp[i].second]<<endl;
lengthStore[temp[i].second] = lengthStore[temp[i].first] + 1;
}
}
//calculate max
// for(int i=0;i<n;i++){
// cout<<lengthStore[i]<<" ";
// }
// cout<<endl;
int mx = -INT_MAX;
for(int i=0;i<n;i++){
if(mx < lengthStore[i]){
mx = lengthStore[i];
}
}
return mx;
}
int main(){
int n;
int arr[1000];
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
int maxCount = LongestAP(arr,n);
cout<<maxCount<<endl;
return 0;
}