Repository navigation
Expand file tree
/
Copy pathlongest_AP_length.cpp
More file actions
54 lines (41 loc) · 1.6 KB
/
Copy pathlongest_AP_length.cpp
File metadata and controls
54 lines (41 loc) · 1.6 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
// works only for sorted arrays
int longest_length_AP(int arr[], int n){
if(n<2){return n;}
int dp[n][n]; // dp[i][j] : length of longest AP subsequence with i as first and j as second element.
int llap = 2;
for(int i=0;i<n-1;i++){dp[i][n-1]=2;} // if n-1 is the 2nd element there can't be any more elements in the AP
// note 0 based indexing has been used in arr[]
// j will be the middle index around which the expansion will take place.
// we will set value corresponding to changes in i & j (not k).
for(int j=n-2;j>=0;j--){
int i=j-1;
int k=j+1;
while(i>=0 && k<=n-1){
if(arr[i] + arr[k] < 2*arr[j]){k++;} // no change in dp[i][j] as only k got changed
else
if(arr[i] + arr[k] > 2*arr[j]){
dp[i][j]=2;
i--;
}
else{
dp[i][j] = 1 + dp[j][k]; // dp[j][k] already filled cz we are moving from right to left
llap = max(llap, dp[i][j]);
i--;k++;
}
}
while(i>=0){ // in case k goes past n-1, we need to set all the i's for
dp[i][j] = 2; // that particular j.
i--;
}
}
return llap;
}
int main(){
int sorted_arr[] = {1, 7, 10, 13, 14, 19};
int n = sizeof(sorted_arr)/sizeof(sorted_arr[0]);
cout<<"length of longest AP: "<<longest_length_AP(sorted_arr, n);
return 0;
}