Replace Elements with Greatest Element on Right Side - Problem

Given an array arr, your task is to transform it by replacing every element with the greatest element among all the elements to its right side. The rightmost element has no elements to its right, so it should be replaced with -1.

For example, if you have the array [17, 18, 5, 4, 6, 1], you need to:

  • Replace 17 with the maximum of [18, 5, 4, 6, 1] which is 18
  • Replace 18 with the maximum of [5, 4, 6, 1] which is 6
  • Replace 5 with the maximum of [4, 6, 1] which is 6
  • Continue this process...
  • Replace the last element 1 with -1

The goal is to return the modified array after all replacements are complete.

Input & Output

example_1.py โ€” Basic Array
$ Input: arr = [17,18,5,4,6,1]
โ€บ Output: [18,6,6,6,1,-1]
๐Ÿ’ก Note: For index 0: max of [18,5,4,6,1] is 18. For index 1: max of [5,4,6,1] is 6. For index 2: max of [4,6,1] is 6. For index 3: max of [6,1] is 6. For index 4: max of [1] is 1. Index 5 becomes -1.
example_2.py โ€” Single Element
$ Input: arr = [400]
โ€บ Output: [-1]
๐Ÿ’ก Note: There's only one element, so it has no elements to its right. According to the problem, it should be replaced with -1.
example_3.py โ€” Decreasing Array
$ Input: arr = [9,7,5,3,1]
โ€บ Output: [7,5,3,1,-1]
๐Ÿ’ก Note: In a decreasing array, each element's replacement is simply the next element to its right, except the last which becomes -1.

Constraints

  • 1 โ‰ค arr.length โ‰ค 104
  • 1 โ‰ค arr[i] โ‰ค 105
  • Follow-up: Could you solve it without using extra space?

Visualization

Tap to expand
Reverse Iteration Approach17185461Original ArrayTraverse Right to Left186661-1Result ArrayStep-by-Step ProcessStep 1: i=5, arr[5]=1 โ†’ arr[5]=-1, max=1Step 2: i=4, arr[4]=6 โ†’ arr[4]=1, max=6Step 3: i=3, arr[3]=4 โ†’ arr[3]=6, max=6Step 4: i=2, arr[2]=5 โ†’ arr[2]=6, max=6Step 5: i=1, arr[1]=18 โ†’ arr[1]=6, max=18Step 6: i=0, arr[0]=17 โ†’ arr[0]=18, max=18Time: O(n) | Space: O(1)๐Ÿ’ก Key InsightBy moving backwards, we build up themaximum as we go, eliminating the needto repeatedly scan future elements!
Understanding the Visualization
1
Start at the End
Begin from the last element. Since it has no elements to its right, replace it with -1. Track the maximum seen so far.
2
Move Backwards
For each element moving right to left, replace it with the current maximum, then update the maximum with the current element's original value.
3
Complete the Process
Continue until you reach the beginning. Each element gets replaced with the maximum of all elements that were originally to its right.
Key Takeaway
๐ŸŽฏ Key Insight: Working backwards allows us to incrementally build the maximum of right-side elements, achieving optimal O(n) time complexity with constant space.
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
68.5K Views
Medium Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen