Replace Elements with Greatest Element on Right Side

EasyArray

Solution

export function replaceElements(arr: number[]): number[] {
  const n = arr.length;
  let maxValue = -1;
  for (let i = n - 1; 0 <= i; i--) {
    [arr[i], maxValue] = [maxValue, Math.max(arr[i], maxValue)];
  }
  return arr;
}