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
4mo

The task is to find the next greater element for each element in an array to its right, if no greater element exists, return -1.

  • Iterate through the array from right to left and use a stack to keep tra...read more

Help your peers!
Select
Add answer anonymously...

JPMorgan Chase & Co. Software Developer interview questions & answers

A Software Developer was asked 1w agoQ. Given a directed graph, determine if it contains a cycle.
A Software Developer was asked 12mo agoQ. Given a Python program, debug the program.
A Software Developer was asked Q. What are the differences between a HashMap and a LinkedHashMap?

Popular interview questions of Software Developer

A Software Developer was asked 12mo agoQ1. Given a Python program, debug the program.
A Software Developer was asked Q2. What are the differences between a HashMap and a LinkedHashMap?
A Software Developer was asked Q3. Given an array of integers nums and an integer target, return indices of the two...read more
JPMorgan Chase & Co. Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits