# SESO36 - Editorial

#### Problem Statement

Given an array of integers, the task is to find the next greater element for each element in the array. The next greater element for an element `x` in the array is the first element on the right side of `x` that is greater than `x`. If no such element exists, we output `-1` for that element.

#### Approach

The approach to finding the next greater element for each element in an array involves using a nested loop where the outer loop iterates through each element, and the inner loop searches for the first greater element to the right of the current element. We initialize a result array with `-1` and, for each element in the outer loop, we traverse the subsequent elements in the inner loop to find the first element greater than the current one. If found, we store this greater element in the result array and break out of the inner loop. This process is repeated for all elements, resulting in a time complexity of O(n^2).

#### Complexity Analysis

• Time Complexity: The time complexity of this solution is O(n^2), where `n` is the number of elements in the array. This is because, for each element, we potentially compare it with every other element to its right.

• Space Complexity: The space complexity is O(n), which is used to store the next greater elements in the `nge` vector. The solution also requires constant extra space for other variables.