Replace elements with greatest element on right side
Link : https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/description/
solution 1
var replaceElements = function (arr) {
if (arr.length === 1) {
return [-1];
}
let max = -1;
let a;
for (let i = arr.length - 1; i >= 0; --i) {
a = arr[i];
arr[i] = max;
if (max < a) {
max = a;
}
}
return arr;
};