Description
Given an array arr, replace every element with the greatest element among the elements to its right, and replace the last element with -1. Return the modified array.
Examples
Input:
arr = [17,18,5,4,6,1]Output:
[18,6,6,6,1,-1]Explanation:
For each position, find max to right.
Input:
arr = [3,1,8,2,7]Output:
[8,8,7,7,-1]Explanation:
At index 0: max(1,8,2,7)=8. At index 1: max(8,2,7)=8. At index 2: max(2,7)=7. At index 3: max(7)=7. At index 4: no elements to right, so -1.
Input:
arr = [9,8,7,6,5]Output:
[8,7,6,5,-1]Explanation:
In a decreasing array, each element is replaced by the next element to its right, since that's the largest remaining element. The last element becomes -1.
Constraints
- •
1 ≤ arr.length ≤ 10⁴