Given an array **arr** of integers of size **N**, the task is to find, for every element, the number of elements that are greater than it.

**Examples:**

Input:arr[] = {4, 6, 2, 1, 8, 7}Output:{3, 2, 4, 5, 0, 1}

Input:arr[] = {2, 3, 4, 5, 6, 7, 8}Output:{6, 5, 4, 3, 2, 1, 0}

**Approach:** Store the frequencies of every array element using a Map. Iterate the Map in reverse and store the sum of the frequency of all previously traversed elements (i.e. elements greater than it) for every element.

Below code is the implementation of the above approach:

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `void` `countOfGreaterElements(` `int` `arr[], ` `int` `n) `
`{ ` ` ` `// Store the frequency of the `
` ` `// array elements `
` ` `map<` `int` `, ` `int` `> mp; `
` ` `for` `(` `int` `i = 0; i < n; i++) { `
` ` `mp[arr[i]]++; `
` ` `} `
` ` ` ` `int` `x = 0; `
` ` `// Store the sum of frequency of elements `
` ` `// greater than the current eleement `
` ` `for` `(` `auto` `it = mp.rbegin(); it != mp.rend(); it++) { `
` ` `int` `temp = it->second; `
` ` `mp[it->first] = x; `
` ` `x += temp; `
` ` `} `
` ` ` ` `for` `(` `int` `i = 0; i < n; i++) `
` ` `cout << mp[arr[i]] << ` `" "` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` ` ` `int` `arr[] = { 7, 9, 5, 2, 1, 3, 4, 8, 6 }; `
` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); `
` ` ` ` `countOfGreaterElements(arr, n); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python 3 implementation of the above approach ` ` ` `def` `countOfGreaterElements(arr, n): `
` ` `# Store the frequency of the `
` ` `# array elements `
` ` `mp ` `=` `{i:` `0` `for` `i ` `in` `range` `(` `1000` `)} `
` ` `for` `i ` `in` `range` `(n): `
` ` `mp[arr[i]] ` `+` `=` `1`
` ` ` ` `x ` `=` `0`
` ` `# Store the sum of frequency of elements `
` ` `# greater than the current eleement `
` ` `p ` `=` `[] `
` ` `q ` `=` `[] `
` ` `m ` `=` `[] `
` ` `for` `key, value ` `in` `mp.items(): `
` ` `m.append([key,value]) `
` ` `m ` `=` `m[::` `-` `1` `] `
` ` ` ` `for` `p ` `in` `m: `
` ` `temp ` `=` `p[` `1` `] `
` ` `mp[p[` `0` `]] ` `=` `x `
` ` `x ` `+` `=` `temp `
` ` ` ` `for` `i ` `in` `range` `(n): `
` ` `print` `(mp[arr[i]],end ` `=` `" "` `) `
` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` `arr ` `=` `[` `7` `, ` `9` `, ` `5` `, ` `2` `, ` `1` `, ` `3` `, ` `4` `, ` `8` `, ` `6` `] `
` ` `n ` `=` `len` `(arr) `
` ` ` ` `countOfGreaterElements(arr, n) `
` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

2 0 4 7 8 6 5 1 3

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count the number of elements which are greater than any of element on right side of an array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements whose sum with K is greater than max element
- Elements greater than the previous and next element in an Array
- Sum of elements in an array with frequencies greater than or equal to that element
- Adding elements of an array until every element becomes greater than or equal to k
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Noble integers in an array (count of greater elements is equal to value)
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count of triplets from the given Array such that sum of any two elements is the third element
- Count elements that are divisible by at-least one element in another array
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Count of smaller elements on right side of each element in an Array using Merge sort
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Find just strictly greater element from first array for each element in second array

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.