forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestArithmeticProgression.java
More file actions
94 lines (78 loc) · 2.8 KB
/
LongestArithmeticProgression.java
File metadata and controls
94 lines (78 loc) · 2.8 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Java program to find Length of the
// Longest AP (llap) in a given sorted set.
import java.io.*;
class GFG
{
// Returns length of the longest
// AP subset in a given set
static int lenghtOfLongestAP(int set[], int n)
{
if (n <= 2) return n;
// Create a table and initialize all
// values as 2. The value ofL[i][j] stores
// LLAP with set[i] and set[j] as first two
// elements of AP. Only valid entries are
// the entries where j>i
int L[][] = new int[n][n];
// Initialize the result
int llap = 2;
// Fill entries in last column as 2.
// There will always be two elements in
// AP with last number of set as second
// element in AP
for (int i = 0; i < n; i++)
L[i][n - 1] = 2;
// Consider every element as second element of AP
for (int j = n - 2; j >= 1; j--)
{
// Search for i and k for j
int i = j -1 , k = j + 1;
while (i >= 0 && k <= n - 1)
{
if (set[i] + set[k] < 2 * set[j])
k++;
// Before changing i, set L[i][j] as 2
else if (set[i] + set[k] > 2 * set[j])
{
L[i][j] = 2; i--;
}
else
{
// Found i and k for j, LLAP with i and j as first two
// elements is equal to LLAP with j and k as first two
// elements plus 1. L[j][k] must have been filled
// before as we run the loop from right side
L[i][j] = L[j][k] + 1;
// Update overall LLAP, if needed
llap = Math.max(llap, L[i][j]);
// Change i and k to fill
// more L[i][j] values for current j
i--; k++;
}
}
// If the loop was stopped due
// to k becoming more than
// n-1, set the remaining
// entties in column j as 2
while (i >= 0)
{
L[i][j] = 2;
i--;
}
}
return llap;
}
// Driver program
public static void main (String[] args)
{
int set1[] = {1, 7, 10, 13, 14, 19};
int n1 = set1.length;
System.out.println ( lenghtOfLongestAP(set1, n1));
int set2[] = {1, 7, 10, 15, 27, 29};
int n2 = set2.length;
System.out.println(lenghtOfLongestAP(set2, n2));
int set3[] = {2, 4, 6, 8, 10};
int n3 = set3.length;
System.out.println(lenghtOfLongestAP(set3, n3)) ;
}
}