Given an array of n integers and q queries, print the number of next greater elements to the right of the given index element.

**Examples:**

Input : a[] = {3, 4, 2, 7, 5, 8, 10, 6} q = 2 index = 0, index = 5 Output: 4 1 Explanation: the next greater elements to the right of 3(index 0) are 4, 7, 8, 10. The next greater elements to the right of 8(index 5) are 10.

A **naive approach** is to iterate for every query from index to end, and find out the number of next greater elements to the right. This won’t be efficient enough as we run two nested loops .

Time complexity: O(n) to answer a query.

Auxiliary space: O(1)

**Better approach** is to store the next greater index of every element and run a loop for every query that iterates from index and keeping the increasing counter as j = next[i]. This will avoid checking all elements and will directly jump to the next greater element of every element. But this won’t be efficient enough in cases like 1 2 3 4 5 6, where the next greater elements are sequentially increasing, ending it up in taking O(n) for every query.

Time complexity : O(n) to answer a query.

Auxiliary space : O(n) for next greater element.

**Efficient approach ** is to store the next greater elements index using next greater element in a next[] array. Then create a dp[] array that starts from n-2, as n-1th index will have no elements to its right and dp[n-1] = 0. While traversing from back we use dynamic programming to count the number of elements to the right where we use memoization as dp[next[i]] which gives us a count of the numbers to the right of the next greater element of the current element, hence we add 1 to it. If next[i]=-1 then we do not have any element to the right hence dp[i]=0. dp[index] stores the count of the number of next greater elements to the right.

Below is the c++ implementation of the above approach

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// array to store the next greater element index ` `void` `fillNext(` `int` `next[], ` `int` `a[], ` `int` `n) ` `{ ` ` ` `// use of stl stack in c++ ` ` ` `stack<` `int` `> s; ` ` ` ` ` `// push the 0th index to the stack ` ` ` `s.push(0); ` ` ` ` ` `// traverse in the loop from 1-nth index ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `// iterate till loop is empty ` ` ` `while` `(!s.empty()) { ` ` ` ` ` `// get the topmost index in the stack ` ` ` `int` `cur = s.top(); ` ` ` ` ` `// if the current element is greater ` ` ` `// then the top index-th element, then ` ` ` `// this will be the next greatest index ` ` ` `// of the top index-th element ` ` ` `if` `(a[cur] < a[i]) { ` ` ` ` ` `// initialize the cur index position's ` ` ` `// next greatest as index ` ` ` `next[cur] = i; ` ` ` ` ` `// pop the cur index as its greater ` ` ` `// element has been found ` ` ` `s.pop(); ` ` ` `} ` ` ` ` ` `// if not greater then break ` ` ` `else` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// push the i index so that its next greatest ` ` ` `// can be found ` ` ` `s.push(i); ` ` ` `} ` ` ` ` ` `// iterate for all other index left inside stack ` ` ` `while` `(!s.empty()) { ` ` ` ` ` `int` `cur = s.top(); ` ` ` ` ` `// mark it as -1 as no element in greater ` ` ` `// then it in right ` ` ` `next[cur] = -1; ` ` ` ` ` `s.pop(); ` ` ` `} ` `} ` ` ` `// function to count the number of next greater numbers to the right ` `void` `count(` `int` `a[], ` `int` `dp[], ` `int` `n) ` `{ ` ` ` `// initializes the next array as 0 ` ` ` `int` `next[n]; ` ` ` `memset` `(next, 0, ` `sizeof` `(next)); ` ` ` ` ` `// calls the function to pre-calculate ` ` ` `// the next greatest element indexes ` ` ` `fillNext(next, a, n); ` ` ` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) { ` ` ` ` ` `// if the i-th element has no next ` ` ` `// greater element to right ` ` ` `if` `(next[i] == -1) ` ` ` `dp[i] = 0; ` ` ` ` ` `// Count of next greater numbers to right. ` ` ` `else` ` ` `dp[i] = 1 + dp[next[i]]; ` ` ` `} ` `} ` ` ` `// answers all queries in O(1) ` `int` `answerQuery(` `int` `dp[], ` `int` `index) ` `{ ` ` ` `// returns the number of next greater ` ` ` `// elements to the right. ` ` ` `return` `dp[index]; ` `} ` ` ` `// driver program to test the above function ` `int` `main() ` `{ ` ` ` `int` `a[] = { 3, 4, 2, 7, 5, 8, 10, 6 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `int` `dp[n]; ` ` ` ` ` `// calls the function to count the number ` ` ` `// of greater elements to the right for ` ` ` `// every element. ` ` ` `count(a, dp, n); ` ` ` ` ` `// query 1 answered ` ` ` `cout << answerQuery(dp, 3) << endl; ` ` ` ` ` `// query 2 answered ` ` ` `cout << answerQuery(dp, 6) << endl; ` ` ` ` ` `// query 3 answered ` ` ` `cout << answerQuery(dp, 1) << endl; ` ` ` ` ` `return` `0; ` `} ` |

Output:

2 0 3

Time complexity: O(1) to answer a query.

Auxiliary Space: O(n)

This article is contributed by **Raja Vikramaditya**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Arithmetic Expression Evalution
- Maximum product of indexes of next greater on left and right
- Longest subsequence such that difference between adjacents is one | Set 2
- Next Greater Frequency Element
- Length of Longest Balanced Subsequence
- Tile Stacking Problem
- Convert array into Zig-Zag fashion
- Iterative Tower of Hanoi
- Largest Rectangular Area in a Histogram | Set 2
- Stack | Set 2 (Infix to Postfix)
- The Stock Span Problem
- The Celebrity Problem
- Next Greater Element
- Reverse a stack using recursion
- Check for balanced parentheses in an expression