
Problem 4: Next Greater Element (easy)
Problem Statement
Given an array, return the Next Greater Element (NGE) for every element, as an array of the same length.
The Next Greater Element for an element x is the first greater element on the right side of x in the array.
Elements for which no greater element exist, consider the next greater element as -1.
Examples
Example 1:
Input: [4, 5, 2, 25]
Output: [5, 25, 25, -1]
Example 2:
Input: [13, 7, 6, 12]
Output: [-1, 12, 12, -1]
Example 3:
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, -1]
Constraints:
- 1 <= arr.length <= 10<sup>4</sup>
- -10<sup>9</sup> <= arr[i] <= 10<sup>9</sup>
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory