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++

`// C++ implementation ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return the count ` `int` `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 code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 2, 1, 2, 3, 4, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << count_greater(arr, n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// function to return the count ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation for the above approach ` `import` `sys ` ` ` `# function to return the count ` `def` `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 code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `3` `, ` `2` `, ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `print` `(count_greater(arr, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to return the count ` `static` `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 code ` `static` `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. ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Smallest Greater Element on Right Side
- Count of smaller elements on right side of each element in an Array using Merge sort
- Find the element before which all the elements are smaller than it, and after which all are greater
- Count greater elements on the left side of every array element
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of array elements which are greater than all elements on its left
- Replace every element of array with sum of elements on its right side
- Replace every element with the greatest element on right side
- Smallest subarray of size greater than K with sum greater than a given value
- Reduce the array by deleting elements which are greater than all elements to its left
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count smaller elements on right side using Set in C++ STL
- Count smaller elements on right side
- Ceiling in right side for every element in an array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.