Skip to content
Related Articles

Related Articles

Number of NGEs to the right

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

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: 6, 1 
Explanation: The next greater elements to the right of 3(index 0) are 4,7,5,8,10,6. 
The next greater elements to the right of 8(index 5) are 10.

Naive Approach: To solve the problem follow the below idea:

Iterate for every query from index to end and find out the number of next greater elements to the right

Below is the implementation of the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of next
// greater elements on the right of
// a given element
int nextGreaterElements(vector<int>& a, int index)
{
    int count = 0, N = a.size();
    for (int i = index + 1; i < N; i++)
        if (a[i] > a[index])
            count++;
 
    return count;
}
 
// Driver's code
int main()
{
 
    vector<int> a = { 3, 4, 2, 7, 5, 8, 10, 6 };
    int Q = 2;
    vector<int> queries = { 0, 5 };
 
    for (int i = 0; i < Q; i++)
        // Function call
        cout << nextGreaterElements(a, queries[i]) << " ";
 
    return 0;
}


Java




// Java code for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find number of next greater elements on
    // the right of a given element
    static int nextGreaterElements(int[] a, int index)
    {
        int count = 0, N = a.length;
        for (int i = index + 1; i < N; i++) {
            if (a[i] > a[index]) {
                count++;
            }
        }
        return count;
    }
 
    public static void main(String[] args)
    {
        int[] a = { 3, 4, 2, 7, 5, 8, 10, 6 };
        int Q = 2;
        int[] queries = { 0, 5 };
 
        for (int i = 0; i < Q; i++) {
            // Function call
            System.out.print(
                nextGreaterElements(a, queries[i]) + " ");
        }
    }
}
 
// This code is contributed by lokeshmvs21.


C#




// C# code for the above approach
using System;
public class GFG {
 
    // Function to find number of next greater elements on
    // the right of a given element
    static int nextGreaterElements(int[] a, int index)
    {
        int count = 0, N = a.Length;
        for (int i = index + 1; i < N; i++) {
            if (a[i] > a[index]) {
                count++;
            }
        }
        return count;
    }
 
    static public void Main()
    {
 
        // Code
        int[] a = { 3, 4, 2, 7, 5, 8, 10, 6 };
        int Q = 2;
        int[] queries = { 0, 5 };
 
        for (int i = 0; i < Q; i++) {
            // Function call
            Console.Write(nextGreaterElements(a, queries[i])
                          + " ");
        }
    }
}
 
// This code is contributed by lokeshmvs21.


Python3




class GFG:
    # Function to find number of next greater elements on
    # the right of a given element
    @staticmethod
    def nextGreaterElements(a,  index):
        count = 0
        N = len(a)
        i = index + 1
        while (i < N):
            if (a[i] > a[index]):
                count += 1
            i += 1
        return count
 
    @staticmethod
    def main(args):
        a = [3, 4, 2, 7, 5, 8, 10, 6]
        Q = 2
        queries = [0, 5]
        i = 0
        while (i < Q):
            # Function call
            print(str(GFG.nextGreaterElements(a, queries[i])) + " ", end="")
            i += 1
 
 
if __name__ == "__main__":
    GFG.main([])


Javascript




// Function to find number of next greater elements on
 // the right of a given element
 function nextGreaterElements(a, index)
 {
     var count = 0;
     var N = a.length;
     var i=0;
     for (i; i < N; i++)
     {
         if (a[i] > a[index])
         {
             count++;
         }
     }
     return count;
 }
  
     var a = [3, 4, 2, 7, 5, 8, 10, 6];
     var Q = 2;
     var queries = [0, 5];
     var i =0;
     for (i; i < Q; i++)
     {
         // Function call
         console.log(nextGreaterElements(a, queries[i]) + " ");
     }
 
// This code is contributed by sourabhdalal0001.


Output

6 1 

Time Complexity: O(NQ), and O(N) to answer a single query
Auxiliary space: O(1) 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!