-
Notifications
You must be signed in to change notification settings - Fork 342
/
Abhinav_LongestAP.cpp
60 lines (46 loc) · 1006 Bytes
/
Abhinav_LongestAP.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
#include <iostream>
using namespace std;
int lenghtOfLongestAP(int set[], int n)
{
if (n <= 2) return n;
int L[n][n];
int llap = 2; // Initialize the result
for (int i = 0; i < n; i++)
L[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 (set[i] + set[k] < 2*set[j])
k++;
else if (set[i] + set[k] > 2*set[j])
{ L[i][j] = 2, i--; }
else
{
L[i][j] = L[j][k] + 1;
llap = max(llap, L[i][j]);
i--; k++;
}
}
while (i >= 0)
{
L[i][j] = 2;
i--;
}
}
return llap;
}
int main()
{
int set1[] = {1, 7, 10, 13, 14, 19};
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[] = {2, 4, 6, 8, 10};
int n3 = sizeof(set3)/sizeof(set3[0]);
cout << lenghtOfLongestAP(set3, n3) << endl;
return 0;
}