JuniorArray

Replace Elements with Greatest Element on Right Side

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

Example 1
Inputarr = [17,18,5,4,6,1]
Output[18,6,6,6,1,-1]
For each index, the value is replaced by the greatest element to its right, and the last element is replaced with -1.
Example 2
Inputarr = [400]
Output[-1]
There are no elements to the right of index 0.

Constraints

  • 1 <= arr.length <= 10^4
  • 1 <= arr[i] <= 10^5

Asked at 4 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate