Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Next Greater Element II

Medium
stackmonotonic-stackarray

Given a circular integer array nums, return for each element the next greater element scanning forward (wrapping around); use -1 if none exists.

Input: one line of space-separated integers. Output: the result array, space-separated.

Example 1
Input:
1 2 1
Output:
2 -1 2

The last 1 wraps around to find 2.

Example 2
Input:
1 2 3 4 3
Output:
2 3 4 -1 4

Constraints

  • 1 ≤ nums.length ≤ 10^4
  • -10^9 ≤ nums[i] ≤ 10^9
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.