-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
81 lines (76 loc) · 1.7 KB
/
code_1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <algorithm>
using namespace std;
int lenOfLongestGP(int set[], int n) {
if (n < 2) {
return n;
}
if (n == 2) {
return (set[1] % set[0] == 0);
}
sort(set, set+n);
int L[n][n];
int llgp = 1;
for (int i = 0; i < n; ++i) {
if (set[n-1] % set[i] == 0) {
L[i][n-1] = 2;
}
else {
L[i][n-1] = 1;
}
}
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] < set[j]*set[j]) {
++k;
}
else if (set[i] * set[k] > set[j]*set[j]) {
if (set[j] % set[i] == 0) {
L[i][j] = 2;
}
else {
L[i][j] = 1;
}
--i;
}
else {
L[i][j] = L[j][k] + 1;
if (L[i][j] > llgp) {
llgp = L[i][j];
}
--i;
++k;
}
}
while (i >= 0) {
if (set[j] % set[i] == 0) {
L[i][j] = 2;
}
else {
L[i][j] = 1;
}
--i;
}
}
return llgp;
}
int main() {
int n;
cout << "\nEnter Size\t:\t";
cin >> n;
int arr[n];
cout << "\nEnter Sequence\n";
for(int i =0; i < n; i++ ) {
cin >> arr[i];
}
cout << "\nLength of LGP\t:\t" << lenOfLongestGP(arr, n) << endl;
return 0;
}