-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLAP.cpp
More file actions
63 lines (56 loc) · 1.11 KB
/
LLAP.cpp
File metadata and controls
63 lines (56 loc) · 1.11 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
55
56
57
58
59
60
61
62
63
#include <climits>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int lenghtOfLongestAP(int *arr, int size)
{
if (size == 2) return size;
vector<vector<int>> dp(size, vector<int>(size));
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
dp[i][j] = 2;
}
}
int max = 2;
for (int j = 1; j < size-1; ++j)
{
int i = j - 1;
int k = j + 1;
while(i >=0 && j < size)
{
int e1 = arr[j]*2;
int e2 = arr[i] + arr[k];
if (e1 == e2)
{
dp[j][k] = dp[i][j] + 1;
max = std::max(max, dp[j][k]);
i--;
k++;
}
else {
if (e1 > e2)
k++;
else
i--;
}
}
}
return max;
}
int main()
{
int set1[] = {1, 7, 10, 15, 27, 29, 33,38,43};
int n1 = sizeof(set1)/sizeof(set1[0]);
cout << lenghtOfLongestAP(set1, n1) << endl;
int set2[] = {1, 7, 10, 15, 27, 29};
int n2 = sizeof(set2)/sizeof(set2[0]);
cout << lenghtOfLongestAP(set2, n2) << endl;
int set3[] = {5,10,15,20,25,30,35};
int n3 = sizeof(set3)/sizeof(set3[0]);
cout << lenghtOfLongestAP(set3, n3) << endl;
getchar();
return 0;
}