Replace Elements with Greatest Element on Right Side (LeetCode 1299): Solution and Explanation

Easy
Array

Replace every element of arr with the largest value that appears anywhere to its right, and set the last element to -1 because nothing follows it. Return the modified array.

Intuition

The direct approach looks at everything to the right of each index and takes the maximum. That repeats almost the same scan n times and costs O(n²).

Notice how the answers relate. The largest value to the right of index i is either arr[i + 1] itself or the largest value to the right of i + 1. So each answer is built from the one next to it, but only if you go from right to left.

Walking leftward, keep one number mx: the largest value among the elements already passed. When you arrive at index i, mx is exactly the answer for i. Write it, then fold the original arr[i] into mx for the indexes further left.

The order inside the loop matters. You must save the old value before overwriting it, because the update needs the original element, not the replacement. Starting mx at -1 handles the last element for free.

Approach: Right-to-left running maximum

  1. Set mx = -1.
  2. Loop i from len(arr) - 1 down to 0.
  3. Save cur = arr[i].
  4. Write arr[i] = mx, the largest value to the right of i.
  5. Update mx = max(mx, cur) so it includes this element for everything further left.
  6. Return arr.

Step-by-step example

arr = [17,18,5,4,6,1]
  1. mx = -1. i = 5: cur = 1, write -1, mx becomes 1.
  2. i = 4: cur = 6, write 1, mx becomes 6.
  3. i = 3: cur = 4, write 6, mx stays 6.
  4. i = 2: cur = 5, write 6, mx stays 6.
  5. i = 1: cur = 18, write 6, mx becomes 18.
  6. i = 0: cur = 17, write 18, mx stays 18. The array is now [18,6,6,6,1,-1].

Code: Python, Java, C++

1class Solution:
2    def replaceElements(self, arr: List[int]) -> List[int]:
3        mx = -1
4        for i in range(len(arr) - 1, -1, -1):
5            cur = arr[i]
6            arr[i] = mx
7            mx = max(mx, cur)
8        return arr
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Right-to-left running maximum O(n)
Suffix maximum array O(n)
Scan the right side for every index O(n²)
Time

O(n)

Each element is visited once, with a constant amount of work per visit.

Space

O(1)

The array is rewritten in place and only mx and cur are stored.

Other approaches

Scan the right side for every index (time O(n²), space O(1))

For each i, compute the max of arr[i + 1:] directly. Easy to write and a good checker, but quadratic, which is about 50 million comparisons at n = 10,000.

Suffix maximum array (time O(n), space O(n))

Build suffixMax where suffixMax[i] is the max of arr[i:], then answer[i] = suffixMax[i + 1]. Same idea as the running max, but it keeps the whole table instead of one number.

Common mistakes

  • Overwriting arr[i] before saving it, so mx is updated with the replacement instead of the original value.
  • Updating mx before writing, which makes each element include itself in its own answer.
  • Walking left to right. The information you need is on the right, so a forward pass cannot build it incrementally.
  • Forgetting the last element must be -1. Initialising mx to -1 covers it without a special case.

Frequently asked questions

Why iterate from right to left?

The answer for each index depends only on elements to its right. Starting at the right end means those elements have already been seen, and a single running maximum summarises them.

Why does mx start at -1?

The last element has nothing to its right and must become -1. Starting mx at -1 writes that value on the first step, and since every arr[i] is at least 1, -1 is replaced as soon as any real value is seen.

Can this be done without extra space?

Yes. The running max solution uses two variables and rewrites the array in place, so its extra space is O(1).

What is the time complexity of Replace Elements with Greatest Element on Right Side?

O(n) time and O(1) extra space, because each index is visited exactly once.