Ctrl K

The Best Is Still Ahead

Easy
You are given an array of positive integers values, say the scores of each day of a trip in order. For every day you want to know the highest score still waiting ahead. Replace each element with the largest of the elements **strictly to its right**. The last element has nothing to its right, so replace it with -1. Return the resulting array.
Example 1
values=[3, 9, 2, 7, 7] → [9, 7, 7, 7, -1]

To the right of 3 the max is 9, of 9 it is 7, of 2 it is 7, of the first 7 it is the second 7, and nothing follows the last one, hence -1.

Example 2
values=[42] → [-1]

The only element is also the last one, so it becomes -1.

Constraints
  • 1 ≤ values.length ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
values=[3, 9, 2, 7, 7]
Expected
[9, 7, 7, 7, -1]

Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.