-
Notifications
You must be signed in to change notification settings - Fork 1
/
LLAP.cpp
62 lines (55 loc) · 1.17 KB
/
LLAP.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
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int lengthOfLongestAP(int A[], int n){
if (n <= 2)
return n;
int mp[n][n];
int ans = 2;
for (int i = 0; i < n; i++)
mp[i][n - 1] = 2;
for (int j = n - 2; j >= 1; j--)
{
int i = j - 1, k = j + 1;
while (i >= 0 && k <= n - 1)
{
if (A[i] + A[k] < 2 * A[j])
k++;
else if (A[i] + A[k] > 2 * A[j])
{
mp[i][j] = 2, i--;
}
else
{
mp[i][j] = mp[j][k] + 1;
ans = max(ans, mp[i][j]);
i--;
k++;
}
}
while (i >= 0)
{
mp[i][j] = 2;
i--;
}
}
return ans;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int A[n];
for (int i = 0; i < n; i++) {
cin >> A[i];
}
Solution ob;
auto ans = ob.lengthOfLongestAP(A, n);
cout << ans << "\n";
}
return 0;
}