Count the number of elements which are greater than any of element on right side of an array

04 Aug, 2021
Given an array Arr[]. The task is to count the number of elements Arr[i] in the given array such that one or more smaller elements are present on the right side of the element Arr[i] in array.
Examples:  
Input: Arr[] = { 3, 9, 4, 6, 7, 5 } Output: 3Numbers that counts are: 9, 6, 7 9 – As all numbers are present after 9 are smaller than 9, 6 – 5 is smaller element present after it, 7 – 5 is smaller element which is present after it.
Input: Arr[] = { 3, 2, 1, 2, 3, 4, 5 } Output: 2  
Approach: Start traversing array from the last till first element of the array. While traversing maintain a min variable which stores the minimum element till now and a counter variable. Compare min variable with the current element. If min variable is smaller than current element Arr[i], increase the counter and if min is greater than Arr[i] then update the min.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation#include <bits/stdc++.h>using namespace std; // function to return the countint count_greater(int arr[], int n){    int min = INT_MAX;    int counter = 0;     // Comparing the given element    // with minimum element till    // occurred till now.    for (int i = n - 1; i >= 0; i--) {        if (arr[i] > min) {            counter++;        }         // Updating the min variable        if (arr[i] <= min) {            min = arr[i];        }    }     return counter;} // Driver codeint main(){    int arr[] = { 3, 2, 1, 2, 3, 4, 5 };    int n = sizeof(arr) / sizeof(int);     cout << count_greater(arr, n) << endl;     return 0;}
// Java implementation of the approachclass GFG{ // function to return the countstatic int count_greater(int arr[], int n){    int min = Integer.MAX_VALUE;    int counter = 0;     // Comparing the given element    // with minimum element till    // occurred till now.    for (int i = n - 1; i >= 0; i--)    {        if (arr[i] > min)        {            counter++;        }         // Updating the min variable        if (arr[i] <= min)        {            min = arr[i];        }    }    return counter;} // Driver codepublic static void main(String[] args){    int arr[] = { 3, 2, 1, 2, 3, 4, 5 };    int n = arr.length;     System.out.println(count_greater(arr, n));}} // This code is contributed by 29AjayKumar
# Python3 implementation for the above approachimport sys # function to return the countdef count_greater(arr, n) :     min = sys.maxsize;    counter = 0;     # Comparing the given element    # with minimum element till    # occurred till now.    for i in range(n - 1, -1, -1) :        if (arr[i] > min) :            counter += 1;             # Updating the min variable        if (arr[i] <= min) :            min = arr[i];     return counter; # Driver codeif __name__ == "__main__" :     arr = [ 3, 2, 1, 2, 3, 4, 5 ];    n = len(arr);     print(count_greater(arr, n));     # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{     // function to return the countstatic int count_greater(int []arr, int n){    int min = int.MaxValue;    int counter = 0;     // Comparing the given element    // with minimum element till    // occurred till now.    for (int i = n - 1; i >= 0; i--)    {        if (arr[i] > min)        {            counter++;        }         // Updating the min variable        if (arr[i] <= min)        {            min = arr[i];        }    }    return counter;} // Driver codestatic public void Main (){    int []arr = { 3, 2, 1, 2, 3, 4, 5 };    int n = arr.Length;         Console.Write(count_greater(arr, n));}} // This code is contributed by ajit.
<script> // Javascript implementation // Function to return the countfunction count_greater(arr, n){    let min = Number.MAX_VALUE;    let counter = 0;     // Comparing the given element    // with minimum element till    // occurred till now.    for(let i = n - 1; i >= 0; i--)    {        if (arr[i] > min)        {            counter++;        }         // Updating the min variable        if (arr[i] <= min)        {            min = arr[i];        }    }    return counter;} // Driver codelet arr = [ 3, 2, 1, 2, 3, 4, 5 ];let n = arr.length; document.write(count_greater(arr, n) + "<br>"); // This code is contributed by rishavmahato348 </script>
2
 
Time Complexity: O(N)Auxiliary Space: O(1)
29AjayKumar
jit_t
ankthon
shubham_singh
rishavmahato348
pankajsharmagfg
Constructive Algorithms
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.