1299. Replace Elements With Greatest Element On Right Side - Explanation

Problem Link

Description

You are 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:

Input: arr = [2,4,5,3,1,2]

Output: [5,5,3,2,2,-1]

Example 2:

Input: arr = [3,3]

Output: [3,-1]

Constraints:

  • 1 <= arr.length <= 10,000
  • 1 <= arr[i] <= 100,000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Array Traversal - Iterating through arrays both forward and backward
  • Suffix Maximum Pattern - Maintaining a running maximum while traversing from right to left

1. Brute Force

Intuition

For each element at index i, we need to find the maximum value among all elements to its right. The most straightforward approach is to scan every element to the right for each position using index j. The last element has no elements to its right, so it becomes -1.

Algorithm

  1. Create a result array ans of the same size as the input.
  2. For each index i, initialize rightMax to -1.
  3. Iterate through all indices j from i + 1 to the end, updating rightMax with the maximum value found.
  4. Store rightMax in ans at position i.
  5. Return the ans array.
class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        n = len(arr)
        ans = [0] * n
        for i in range(n):
            rightMax = -1
            for j in range(i + 1, n):
                rightMax = max(rightMax, arr[j])
            ans[i] = rightMax
        return ans

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(1)O(1)

2. Suffix Max

Intuition

By traversing right to left, we can maintain a running maximum of all elements seen so far in rightMax. When we visit position i, the current running maximum represents the greatest element to the right of i. We then update rightMax to include arr[i] for the next iteration. This eliminates redundant scanning.

Algorithm

  1. Create a result array ans of the same size as the input.
  2. Initialize rightMax to -1 (the value for the last position).
  3. Traverse the array from right to left using index i.
  4. For each index i, store the current rightMax in ans.
  5. Update rightMax to be the maximum of itself and arr[i].
  6. Return the ans array.
class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        n = len(arr)
        ans = [0] * n
        rightMax = -1
        for i in range(n - 1, -1, -1):
            ans[i] = rightMax
            rightMax = max(arr[i], rightMax)
        return ans

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

Common Pitfalls

Traversing Left to Right

Processing the array from left to right requires recalculating the maximum of all right elements for each position, resulting in O(n^2) time complexity. The optimal approach traverses right to left, maintaining a running maximum in a single pass.

Updating the Maximum Before Storing the Result

When traversing right to left, the current element's value must be stored in the result before updating rightMax. Updating rightMax first causes the current element to incorrectly include itself in its own replacement value.