Replace Elements with Greatest on Right
Given an array
arr.
For each element, replace it with the largest element among all elements to its right.
Replace the last element with -1.After the transformations, return the modified array.
Example 1
Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]
Explanation: Going right to left: maxRight for each position gives the resulting array [18,6,6,6,1,-1].
Output: [18,6,6,6,1,-1]
Explanation: Going right to left: maxRight for each position gives the resulting array [18,6,6,6,1,-1].
Example 2
Input: arr = [5]
Output: [-1]
Explanation: There are no elements to the right, so the element is replaced with -1 (no maximum on the right).
Output: [-1]
Explanation: There are no elements to the right, so the element is replaced with -1 (no maximum on the right).
Example 3
Input: arr = [2,3,1,-1]
Output: [-1,3,3,2]
Explanation: Traverse right to left, tracking the maximum to the right: replacement result [ -1,3,3,2 ].
Output: [-1,3,3,2]
Explanation: Traverse right to left, tracking the maximum to the right: replacement result [ -1,3,3,2 ].
Line 1, Char 1