Next Greater Element Problem Statement

Given a list of integers of size N, your task is to determine the Next Greater Element (NGE) for every element. The Next Greater Element for an element X is the first element to the right of X in the list that is greater than X. If no such element exists, the Next Greater Element should be considered as -1.

Example:

Input:
[7, 12, 1, 20]
Output:
[12, 20, 20, -1]
Explanation:

For the given list:

  • The next greater element for 7 is 12.
  • The next greater element for 12 is 20.
  • The next greater element for 1 is 20.
  • There is no greater element for 20, so it is -1.

Constraints:

  • 1 <= N <= 10^5
  • 1 <= ARR[i] <= 10^9
  • Time Limit: 1 sec
Input:
The first line of input contains an integer 'N' denoting the size of the array/list.
The second line contains 'N' space-separated integers denoting the elements of the array/list.
Output:
A single line containing 'N' space-separated integers denoting the Next Greater Element for each corresponding element in the array/list.
Note:
You are not required to print anything manually; the function implementation already manages output.
AnswerBot
4mo

Find the Next Greater Element for each element in a list of integers.

  • Iterate through the list of integers from right to left.

  • Use a stack to keep track of elements whose NGE is yet to be found.

  • Pop elem...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Software Developer

A Software Developer was asked Q1. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more
A Software Developer was asked Q2. How does Facebook store comments in its database?
A Software Developer was asked Q3. Convert a Number to Words Given an integer number num, your task is to convert '...read more
Clearwater Analytics 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