Skip to main content

Replace Elements with Greatest Element on Right Side - Solution & Explanation

EasyArray13 min readAsked at: Amazon, Meta, Google +1
Practice this problem

Problem Statement

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 <= 104
  • 1 <= arr[i] <= 105

Approach Overview

Problem 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 1: Reverse Iteration Approach

This approach iterates over the array starting from the end toward the beginning. By doing so, we can keep a running track of the maximum element found so far on the right side. This method allows us to update each element in a single pass efficiently.

In this C code, we iterate from the end of the array, keeping track of the maximum value seen so far. We replace each index with the maximum collected, starting with -1 for the last element.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the number of elements in the array.
Space Complexity: O(1), as the operation is done in-place with a constant extra space.

Try this approach in the editor →

Approach 2: Brute Force Approach

This approach involves nested loops, where for each element, we check its following elements to find the greatest and update it. Although this is not the most efficient approach, it's easier to understand.

This C implementation uses a brute force method where each element's right subarray is checked to find the maximum element, thus resulting in a higher time complexity.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) because of the nested loops over the array.
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Reverse Traversal

We use a variable mx to record the maximum value to the right of the current position, initially mx = -1.

Then we traverse the array from right to left. For each position i, we denote the current value as x, update the current position's value to mx, and then update mx = max(mx, x).

Finally, return the original array.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Reverse Iteration Approach

Time Complexity: O(n) where n is the number of elements in the array.
Space Complexity: O(1), as the operation is done in-place with a constant extra space.

Brute Force Approach

Time Complexity: O(n^2) because of the nested loops over the array.
Space Complexity: O(1)

Reverse Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ApproachO(n^2)O(1)Useful for understanding the problem or when constraints are very small.
Reverse Iteration ApproachO(n)O(1)Best choice for interviews and production. Maintains a running suffix maximum while traversing from right to left.

Video Solution

Leetcode 1299 - REPLACE ELEMENTS WITH GREATEST ELEMENT ON RIGHT SIDE • NeetCode • 75,869 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Replace Elements with Greatest Element on Right Side easy or hard?
The problem is categorized as Easy because it mainly tests array traversal and recognizing a suffix maximum pattern. Once you realize that scanning from the right avoids repeated work, the implementation becomes a short linear pass.
Replace Elements with Greatest Element on Right Side Python/Java solution
The typical Python or Java solution iterates from the last index to the first while maintaining a maxRight variable. At each step, the element is replaced with maxRight and the variable is updated with the maximum of the current element and maxRight. This results in O(n) time and constant space.
How to solve Replace Elements with Greatest Element on Right Side in O(n)?
Traverse the array from right to left while keeping a variable called maxRight. Replace the current element with maxRight, then update maxRight using the original value of that element. This maintains the maximum of all elements to the right without rescanning the array.
What is the best approach for Replace Elements with Greatest Element on Right Side?
The best approach is reverse iteration from the end of the array while maintaining the maximum value seen so far. For each index, replace the current element with the stored maximum and then update the maximum if the current element is larger. This processes each element once, resulting in O(n) time and O(1) extra space.
Is Replace Elements with Greatest Element on Right Side asked at Google/Amazon/Meta?
Array transformation and suffix maximum problems appear frequently in interviews at companies like Amazon and Google. While this exact question may not always appear verbatim, the reverse traversal and running maximum pattern is commonly tested in coding interviews.
What data structure is used in Replace Elements with Greatest Element on Right Side?
The problem primarily uses a simple array with a running variable to track the suffix maximum. No additional data structures like stacks or hash maps are required in the optimal solution, which keeps the space complexity at O(1).
What is the time complexity of Replace Elements with Greatest Element on Right Side?
The optimal solution runs in O(n) time because the array is traversed exactly once from right to left. A running variable stores the maximum value seen so far. The brute force approach takes O(n^2) time since it scans the remaining elements for every index.

Ready to solve this problem?

Practice Replace Elements with Greatest Element on Right Side with our built-in code editor and test cases.

Practice on FleetCode