Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given an array * 'a'* of size

Print the Next Greater Element(NGE) for every element.

The Next Greater Element for an element 'x' is the first element on the right side of 'x' in the array, which is greater than 'x'.

If no greater elements exist to the right of 'x', consider the next greater element as -1.

```
Input: 'a' = [7, 12, 1, 20]
Output: NGE = [12, 20, 20, -1]
Explanation: For the given array,
- 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 on the right side. So we consider NGE as -1.
```

Detailed explanation

```
5
1 5 3 4 2
```

```
5 -1 4 -1 -1
```

```
5
5 5 5 5 5
```

```
-1 -1 -1 -1 -1
```

```
The expected time complexity is O(n).
```

```
1 <= 'n' <= 10^5
1 <= 'a[i]' <= 10^9
Time Limit: 1 sec
```