Practice arena
C++17·2 s·128 MB
LinearIntermediate

Next greater value

For every element, find the first strictly greater value to its right. Print -1 when none exists.

Input

  • The first line contains T.
  • Each case contains n followed by n integers.

Output

Print n next-greater values for each case.

Technique

monotonic stack

Target

O(n)

Constraints & sample
  • 1 ≤ n ≤ 200,000
  • The sum of n is at most 300,000

Sample input

2
4
4 5 2 25
4
13 7 6 12

Expected output

5 25 25 -1
-1 12 12 -1
Review the concept

Before you submit

  • Test the smallest valid input and one awkward edge case.
  • Keep the solution within O(n).
  • Match spacing and line breaks exactly before checking 7 hidden cases.
main.cpp
Draft saved locally · 471 / 30,000
Ln 1, Col 1Spaces: 4UTF-8LFC++17

Visible testcases

Editable and stored only in this browser. Hidden judge cases never change.

Checking your session…

Run checks Sample · Submit checks 7 hidden cases

Run the sample first, then send your solution through 7 hidden cases.

Code runs in an isolated Judge0 environment. Drafts and custom visible cases stay on this device; submission metadata and progress sync to MongoDB.