Close
Topic list
Next Greater Element II
MEDIUM
35 mins
Stacks & Queues
Topics (Covered in this problem)
Problem solved
Skill meter
Stacks & Queues
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Next Greater Element II

Contributed by
Ratnesh
Medium
0/80
Avg time to solve 35 mins
Success Rate 70 %
Share

## Problem Statement

#### Note: 1-based indexing is used for the explanations.

##### Example:
``````Input: [1,2,1]

Output: 2 -1 2

The first greater element next to 1 is 2.
There is no greater element next to 2 while traversing circularly, hence answer is -1.
The first greater element next to 1 is 2.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
3
1 2 1
5
1 2 3 4 3
``````

##### Sample Output 1 :

``````2 -1 2
2 3 4 -1 4
``````
##### Explanation Of Sample Input 1 :
``````For the first case:

Input: [1,2,1]

Output: 2 -1 2

The first greater element next to 1 is 2(index 2).
There is no greater element next to 2 while traversing circularly, hence answer is -1.
The first greater element next to 1 is 2(index 2).

For the second case:

Input: [1,2,3,4,3]

Output: 2 3 4 -1 4

The first greater element next to 1 is 2(index 2).
The first greater element next to 2 is 3(index 3).
The first greater element next to 3 is 4(index 4).
There is no greater element next to 4 while traversing circularly, hence answer is -1.
The first greater element next to 3 is 4(index 3).
``````
##### Sample Input 2 :
``````2
8
6 1 50 1 50 30 6 100
6
55 19 21 13 10 67
``````
##### Sample Output 2 :
``````50 50 100 50 100 100 100 -1
67 21 67 67 67 -1
``````
Auto
Console