Replace Elements with Greatest Element on Right Side
Replace each number in an array with the largest number to its right, in one pass from the right end.
You are given an array of positive integers arr
Example 1
Output: [18, 6, 6, 6, 1, -1]
Explanation: after 17 the largest number is 18, after 18 it is 6, and so on.
Example 2
Output: [-1]
The Idea
Picture six people standing in a row, each holding a number card: 17, 18, 5, 4, 6 and 1. Each person wants to know the biggest card held by anyone to their right. The person holding 17 sees 18, 5, 4, 6 and 1, so their answer is 18. The person holding 5 sees 4, 6 and 1, so their answer is 6. The last person has nobody to the right and gets -1.
If every person looks at everyone to their right, the six people make 5 + 4 + 3 + 2 + 1 = 15 looks. That is fine for six, but 100,000 numbers would need about 5 billion looks. Does each person really have to look at everyone to the right?
No. Look at the picture below. The top row is the array, and the bottom row is the answer for each position, numbered i = 0i = 5
Take the person holding 17, at i = 0
Every answer works the same way, as the other arrows show. It is the larger of two boxes on its right: the array value next to it, and the answer next to it. So starting at the right end with -1 and walking left, each answer takes one step to work out.
On the walk, only one number has to be remembered: the largest value seen so far. A value carried along like this is called a running maximum.
Solution Approach
Turning that idea into code means settling three things: which way to walk, what the running maximum starts at, and how to write the answers into the same array.
Walking from the right
What does the simple way cost? It uses two loops, one inside the other: the outer loop goes over each position from the left, and the inner loop scans everything to its right. That is the 15 looks from before, and the work grows with the square of the array's length. Could one walk from the left, carrying a single number, do better? No, because each answer depends on numbers that walk has not reached yet. Walking from the right, every value the next answer needs has already been passed.
The starting value
What should the running maximum hold before the walk begins? The last position has nothing to its right and needs -1, so the running maximum starts at -1. That works because all the numbers are positive: any number in the array is larger than -1 and replaces it at once. If the array could hold negative numbers, the -1 would win against them, and a different start would be needed.
Writing the answers into the same array
Can the answers go straight into arr
Java Solution
The method replaceElements
- First, set
, the running maximum, to -1, the answer for the last position.maxRight - Walk the array from right to left with one loop, moving
from the last index down to 0.i - Inside the loop, save
inarr[i] , then writecurrent intomaxRight .arr[i] - Then set
to the larger ofmaxRight andmaxRight , so the position to the left sees this number too.current - When the loop is done, return
, which now holds the answers.arr
The main
import java.util.Arrays;
public class ReplaceElementsWithGreatestFromRight {
public static int[] replaceElements(int[] arr) {
int maxRight = -1; // largest value to the right; -1 for the last slot
for (int i = arr.length - 1; i >= 0; i--) {
int current = arr[i]; // save it before it is overwritten
arr[i] = maxRight;
maxRight = Math.max(maxRight, current);
}
return arr;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(replaceElements(new int[] {17, 18, 5, 4, 6, 1})));
System.out.println(Arrays.toString(replaceElements(new int[] {400})));
System.out.println(Arrays.toString(replaceElements(new int[] {9, 7, 3, 2})));
System.out.println(Arrays.toString(replaceElements(new int[] {1, 2, 3, 4})));
System.out.println(Arrays.toString(replaceElements(new int[] {5, 5, 5})));
}
}
Running it prints:
[18, 6, 6, 6, 1, -1]
[-1]
[7, 3, 2, -1]
[4, 4, 4, -1]
[5, 5, -1]
[18, 6, 6, 6, 1, -1][400][-1][7, 3, 2, -1][4, 4, 4, -1]
The last line, [5, 5, -1]
The order of the last two lines in the loop matters. Updating first, with maxRight = Math.max(maxRight, arr[i])arr[i] = maxRight[18, 18, 6, 6, 6, 1]
The original implementation,
Complexity
The method makes one pass over the array and keeps two numbers besides the index.
- Time: O(n), for an array of n numbers, because the loop visits each position once and does a read, a write and one
there.Math.max - Space: O(1). The answers are written into
itself, and onlyarr andmaxRight are kept.current
A running maximum from the right is also one half of