1
class Solution {
2
public:
3
vector<int> replaceElements(vector<int> &arr) {
4
int n = arr.size();
5

6
// taking last index as greatest for now
7
int g = arr[n - 1];
8
// setting last index as -1
9
arr[n - 1] = -1;
10
for (int i = n - 2; i >= 0; i--) {
11
// storing last index value to be changed to comapare with the greatest
12
// value till now
13
int h = arr[i];
14
// assigning greatest till now from right
15
arr[i] = g;
16
// checking if current is greater than the previous indices
17
if (h > g) {
18
g = h;
19
}
20
}
21
// returning the value
22
return arr;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0