C++ Program To Find Next Greater Element

Last Updated : 25 Aug, 2026

Given an array, the Next Greater Element (NGE) for an element is the first element greater than it on its right side. If no greater element exists, the NGE is -1.

  • For the rightmost element, the NGE is always -1.
  • A stack-based approach can find the NGE for all elements in O(n) time.

Example:

Input: arr[] = {11, 13, 21, 3}

Output:

11 --> 13
13 --> 21
21 --> -1
3 --> -1

Approaches To Find Next Greater Element

The Next Greater Element can be found using a simple brute-force approach or an efficient stack-based approach.

1. Brute Force

The brute-force approach uses two nested loops. For each element, the second loop checks the elements on its right and stops as soon as the first greater element is found.

Steps:

  • Traverse the array using an outer loop.
  • For each element, check all elements to its right.
  • Store the first element greater than the current element.
  • If no greater element is found, use -1.
C++
#include <iostream>
using namespace std;

// Function to find the Next Greater Element
void printNGE(int arr[], int n)
{
    // Traverse every element
    for (int i = 0; i < n; i++)
    {
        int next = -1;

        // Check elements on the right
        for (int j = i + 1; j < n; j++)
        {
            // First greater element is the NGE
            if (arr[j] > arr[i])
            {
                next = arr[j];
                break;
            }
        }

        // Print the element and its NGE
        cout << arr[i] << " --> " << next << '\n';
    }
}

int main()
{
    int arr[] = {11, 13, 21, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Find and print NGEs
    printNGE(arr, n);

    return 0;
} 

Output
11 --> 13
13 --> 21
21 --> -1
3 --> -1

Explanation

  • The outer loop selects each array element one by one.
  • The inner loop searches for the first greater element on its right and stops as soon as it is found.
  • If no greater element exists, -1 is printed.

2. Using a Stack

A stack can be used to efficiently find the Next Greater Element. The stack stores elements whose NGE has not been found yet. For each new element, smaller elements at the top of the stack have found their NGE because the current element is the first greater element encountered for them.

NextGreaterElement

Steps:

  • Traverse the array from left to right.
  • For the current element, compare it with the top element of the stack.
  • While the current element is greater than the stack top, the current element becomes the NGE of the popped element.
  • Push the current element onto the stack.
  • After traversal, all remaining elements have no greater element, so their NGE is -1.
C++
#include <iostream>
#include <stack>
using namespace std;

// Function to find the Next Greater Element
void printNGE(int arr[], int n)
{
    stack<int> s;

    // Process every element
    for (int i = 0; i < n; i++)
    {
        // Resolve NGEs for smaller elements
        while (!s.empty() && s.top() < arr[i])
        {
            cout << s.top() << " --> " << arr[i] << '\n';
            s.pop();
        }

        // Store current element for future comparisons
        s.push(arr[i]);
    }

    // Remaining elements have no greater element
    while (!s.empty())
    {
        cout << s.top() << " --> -1\n";
        s.pop();
    }
}

int main()
{
    int arr[] = {11, 13, 21, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Find and print NGEs
    printNGE(arr, n);

    return 0;
}

Output
11 --> 13
13 --> 21
3 --> -1
21 --> -1

Explanation

  • The stack stores elements that are still waiting for their Next Greater Element.
  • When a larger element is found, it becomes the NGE for all smaller elements removed from the stack.
  • Elements left in the stack after traversal do not have a greater element on their right, so their NGE is -1.

3. Using a Stack with Output in Input Order

To preserve the original order, traverse the array from right to left and store each NGE in a result array.

Steps:

  • Traverse the array from right to left.
  • Remove all elements from the stack that are smaller than or equal to the current element.
  • The stack top, if present, is the NGE of the current element.
  • Store the NGE at the current index in the result array.
  • Push the current element onto the stack.
  • Traverse the result array from left to right to print the answers.
C++
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

// Function to find NGEs in the same order as input
void printNGE(int arr[], int n)
{
    stack<int> s;
    vector<int> result(n);

    // Traverse from right to left
    for (int i = n - 1; i >= 0; i--)
    {
        // Remove elements that cannot be the NGE
        while (!s.empty() && s.top() <= arr[i])
            s.pop();

        // Stack top is the next greater element
        result[i] = s.empty() ? -1 : s.top();

        // Store current element for future elements
        s.push(arr[i]);
    }

    // Print results in the original order
    for (int i = 0; i < n; i++)
        cout << arr[i] << " --> " << result[i] << '\n';
}

int main()
{
    int arr[] = {11, 13, 21, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Find and print NGEs
    printNGE(arr, n);

    return 0;
} 

Output
11 --> 13
13 --> 21
21 --> -1
3 --> -1

Explanation

  • Traverse the array from right to left and remove all smaller or equal elements from the stack.
  • The remaining stack top is the next greater element; store it at the current index and push the current element.
Comment