Next Greater Element Problem Statement
You are given an array arr
of length N
. For each element in the array, find the next greater element (NGE) that appears to the right. If there is no such greater element, return -1.
Input:
T
N
arr[0] arr[1] ... arr[N-1]
Output:
NGE[0] NGE[1] ... NGE[N-1]
Example:
Input:
1
3
1 3 2
Output:
3 -1 -1
Explanation:
For 1
, 3
is the next greater element. 3
has no greater element to its right, so it returns -1
. Similarly, 2
also returns -1
.
Constraints:
1 <= T <= 10
1 <= N <= 10^5
0 <= arr[i] <= 10^9
- Time Limit:
1 sec
Note:
Implement the function without printing anything; printing is already handled.
AnswerBot
3d
Given an array, find the next greater element to the right for each element. If no greater element, return -1.
Use a stack to keep track of elements in decreasing order.
Iterate through the array from r...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Developer Intern
>
Across The Globe Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app