-
Notifications
You must be signed in to change notification settings - Fork 4
/
replace-every-elem-with-greatest-right.cpp
77 lines (61 loc) · 1.27 KB
/
replace-every-elem-with-greatest-right.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
#include <bits/stdc++.h>
#define _DEBUG
using namespace std;
const int MAX = 1e7 + 1;
long long li[MAX];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> li[i];
int start = 0;
int end = n - 1;
int diff = -1;
while (start < end) {
if (li[start] <= li[end]) {
diff = end - start;
break;
} else {
if ((li[start] - li[start + 1]) > (li[end - 1] - li[end])) start++;
else end--;
}
}
cout << diff << endl;
}
void solve_geek_for_geeks() {
// https://www.geeksforgeeks.org/given-an-array-arr-find-the-maximum-j-i-such-that-arrj-arri/
}
// https://www.geeksforgeeks.org/replace-every-element-with-the-greatest-on-right-side/
void solve2() {
int n;
cin >> n;
int holder;
vector<int> li(n);
for (int i = 0; i < n; i++) {
cin >> holder;
li[i] = holder;
}
int current = -1;
int nextMax = -1;
for (int i = n - 1; i >= 0; i--) {
current = max(nextMax, current);
nextMax = max(nextMax, li[i]);
li[i] = current;
}
for (int i = 0; i < n; i++)
cout << li[i] << " ";
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
int no_of_test_cases = 1;
// cin >> no_of_test_cases;
while (no_of_test_cases--) {
// solve();
// solve_geek_for_geeks()
solve2();
}
return 0;
}