• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
January 06, 2024 |640 Views
SDE Sheet - Next Larger Element
  Share   Like
Description
Discussion

This video is part of Stack section under GFG SDE Sheet.

In this problem, we are given an array arr[ ] of size N having elements, the task is to find the next greater element for each element of the array in order of their appearance in the array.
Next greater element of an element in the array is the nearest element on the right which is greater than the current element.
If there does not exist next greater of current element, then next greater element for current element is -1. For example, next greater of the last element is always -1.

Example :

Input: 
N = 4, arr[] = [1 3 2 4]
Output:
3 4 4 -1

Explanation:
In the array, the next larger element to 1 is 3 , 3 is 4 , 2 is 4 and for 4 ? since it doesn't exist, it is -1.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-
Article: https://www.geeksforgeeks.org/next-greater-element/
Problem: https://www.geeksforgeeks.org/problems/next-larger-element-1587115620/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More