Watch 10 video solutions for Replace Elements with Greatest Element on Right Side, a easy level problem involving Array. This walkthrough by NeetCode has 71,428 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). - index 3 --> the greatest element to the right of index 3 is index 4 (6). - index 4 --> the greatest element to the right of index 4 is index 5 (1). - index 5 --> there are no elements to the right of index 5, so we put -1.
Example 2:
Input: arr = [400] Output: [-1] Explanation: There are no elements to the right of index 0.
Constraints:
1 <= arr.length <= 1041 <= arr[i] <= 105Problem Overview: You are given an integer array arr. For every index, replace the value with the greatest element that appears to its right. The last element has no elements to its right, so it becomes -1. The goal is to compute the transformation efficiently.
Approach 1: Brute Force Approach (Time: O(n^2), Space: O(1))
For each index i, scan the subarray from i+1 to the end and compute the maximum value. Replace arr[i] with that maximum. The final element is set to -1. This method relies purely on repeated traversal of the array, making it straightforward to reason about but inefficient for large inputs.
The key drawback is repeated work. Every element recomputes the maximum of the suffix even though nearby elements share most of the same suffix values. That repeated scanning results in quadratic time complexity.
Approach 2: Reverse Iteration Approach (Time: O(n), Space: O(1))
The optimal solution processes the array from right to left while maintaining the maximum value seen so far. Start with maxRight = -1. For each index moving backward, store the current element, replace it with maxRight, then update maxRight = max(maxRight, originalValue).
This works because when you traverse from the end, maxRight always represents the largest element among all elements to the right of the current index. Each element is processed exactly once, and the algorithm only stores a single running maximum. The technique is common in array problems where suffix information is needed without extra memory.
The approach modifies the array in-place and avoids additional data structures. It behaves similarly to a suffix maximum computation often seen in prefix/suffix style array problems.
Recommended for interviews: Interviewers expect the reverse iteration solution. It shows you recognize that the maximum of the suffix can be maintained incrementally instead of recomputed. Starting with the brute force idea demonstrates understanding of the requirement, but transitioning to the O(n) reverse traversal shows strong optimization instincts in array manipulation problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Approach | O(n^2) | O(1) | Useful for understanding the problem or when constraints are very small. |
| Reverse Iteration Approach | O(n) | O(1) | Best choice for interviews and production. Maintains a running suffix maximum while traversing from right to left. |