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
1y
The task is to find the next greater element for each element in the given array.
Iterate through the array from right to left.
Use a stack to keep track of the next greater element.
Pop elements from th...read more
Hishaamudin Z
1y
t= int(input()) for _ in range(t): arr = list(map(int,input().split())) n = len(arr) res = [] curr = arr[0] for i in range(1,n): temp = -1 if (curr<arr[i]): temp = arr[i] res.append(temp) curr = arr[i...read more
Help your peers!
Add answer anonymously...
Sigmoid Data Engineer interview questions & answers
A Data Engineer was asked 9mo agoQ. How would you sort an array containing only 0s and 1s?
A Data Engineer was asked 9mo agoQ. Given a matrix, traverse and print its elements in a spiral manner.
A Data Engineer was asked 12mo agoQ. Given the head of a singly linked list, determine if the linked list has a cycle...read more
Popular interview questions of Data Engineer
A Data Engineer was asked 9mo agoQ1. How would you sort an array containing only 0s and 1s?
A Data Engineer was asked 9mo agoQ2. Given a matrix, traverse and print its elements in a spiral manner.
A Data Engineer was asked 12mo agoQ3. Given the head of a singly linked list, determine if the linked list has a cycle...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

