-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALTARAY.cpp
More file actions
55 lines (51 loc) · 1.11 KB
/
ALTARAY.cpp
File metadata and controls
55 lines (51 loc) · 1.11 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
// https://www.codechef.com/problems/ALTARAY
#include <iostream>
int M[100001];
int arr[100001] = {0};
int calc_len(int i, int n) {
// Check memory
if (M[i] > 0) {
return M[i];
}
// Base case
if (i+1 == n) { // End of input
return 1;
}
if (arr[i] * arr[i+1] > 0) {
// Signs are same
return 1;
} else {
// signs are diffenent
int temp = 1 + calc_len(i+1, n);
M[i] = temp;
return temp;
}
}
int main(int argc, char const *argv[])
{
int T;
int N;
int temp;
std::cin >> T;
while (T--) {
std::cin >> N;
for (int i = 0; i < N; i++) {
std::cin >> temp;
if (temp < 0)
arr[i] = -1;
else
arr[i] = 1;
arr[i+1] = 0;
M[i] = -1;
}
for (int i = 0; i < N; i++) {
// std::cout << i << " " << calc_len(i, N) << std::endl;;
std::cout << calc_len(i,N);
if (i + 1 == N)
std::cout << "\n";
else
std::cout << " ";
}
}
return 0;
}