Ctrl K

Next Larger Around the Circle

Medium
The numbers values are arranged in a circle: the first element comes right after the last one. For each element, find the first number strictly larger than it when you move forward around the circle from that element. If there is none, use -1. Return the answers as an array of the same length, in the same order as values.
Example 1
values=[4, 9, 2, 6] → [9, -1, 6, 9]

The 4 is followed right away by 9. Nothing beats 9, so it gets -1. The 2 is followed by 6. After 6 the circle goes on to 4 and then 9, so its answer is 9.

Example 2
values=[7, 7, 3] → [-1, -1, 7]

An equal value does not count as larger, so both 7s get -1. For the 3, the circle continues to a 7.

Example 3
values=[-5] → [-1]
Constraints
  • 1 ≤ len(values) ≤ 10⁴
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPreorder of a Search Tree MediumLow, Peak, Middle MediumFix the Middle of the Line MediumPeak-Split Tree MediumWaiting for a Higher Price Medium
loading Python…
Input
values=[4, 9, 2, 6]
Expected
[9, -1, 6, 9]

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