P17 · 1 second · 256 MB

Next Greater

For each index `i` of `a`, find the next **strictly greater** value to the right. If none exists, that answer is `-1`. Return an array of those values (not indices).

function nextGreater(a) → number[]

CONSTRAINTS

  • 0 ≤ a.length ≤ 5000
  • -10⁹ ≤ a[i] ≤ 10⁹
Input: a = [2, 1, 2, 4]
Output: [4, 2, 4, -1]
Input: a = [4, 3, 2, 1]
Output: [-1, -1, -1, -1]
Strictly decreasing — nothing to the right is greater.
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.