-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdivisible_sequence.cpp
57 lines (42 loc) · 1.14 KB
/
divisible_sequence.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
#include <algorithm>
#include <iostream>
#include <vector>
int get_current_divisible_fragment_length(int t[], int i) {
int current_divisible_fragment = 1;
for (int j = i - 1; j >= 0; j--) {
if (!t[j] || t[i] % t[j]) {
return current_divisible_fragment;
}
++current_divisible_fragment;
}
return current_divisible_fragment;
}
int get_max_divisible_fragment(int t[], int n) {
int max_divisible_fragment = 0;
int i = n - 1;
while (i >= 1) {
int current_divisible_fragment =
get_current_divisible_fragment_length(t, i);
max_divisible_fragment =
std::max(current_divisible_fragment, max_divisible_fragment);
i -= current_divisible_fragment;
}
return max_divisible_fragment;
}
int main() {
int n;
std::cin >> n;
if (n <= 0) {
std::cout << "n must be a positive integer" << std::endl;
return 1;
}
int *t = new int[n];
int value;
for (int i = 0; i < n; i++) {
std::cin >> value;
t[i] = value;
}
std::cout << get_max_divisible_fragment(t, n);
delete[] t;
return 0;
}