Write a function to count the number of smaller elements on the right of each element in an array. Given an unsorted array arr[] of distinct integers, construct another array countSmaller[] such that countSmaller[i] contains the count of smaller elements on right side of each element arr[i] in the array.

Examples:

Input : arr[] = {12, 1, 2, 3, 0, 11, 4} Output :countSmaller[] = {6, 1, 1, 1, 0, 1, 0} Input :arr[]={5, 4, 3, 2, 1} Output :countSmaller[]={4, 3, 2, 1, 0}

In this post an easy implementation of https://www.geeksforgeeks.org/count-smaller-elements-on-right-side/amp/ is discussed.

Create an empty Set in C++ STL (Note that Set in C++ STL is implemented Self Balancing Binary Search Tree).

- Traverse the array element from i=len-1 to 0 and insert every element in a set.
- Find the first element that is lower than A[i] using lower_bound function.
- Find the distance between above found element and the beginning of the set using distance function.
- Store the distance in another array Lets say CountSmaller.
- Print that array .

`// CPP program to find count of smaller ` `// elements on right side using set in C++ ` `// STL. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
`void` `countSmallerRight(` `int` `A[], ` `int` `len) `
`{ ` ` ` `set<` `int` `> s; `
` ` `int` `countSmaller[len]; `
` ` `for` `(` `int` `i = len - 1; i >= 0; i--) { `
` ` `s.insert(A[i]); `
` ` `auto` `it = s.lower_bound(A[i]); `
` ` `countSmaller[i] = distance(s.begin(), it); `
` ` `} `
` ` ` ` `for` `(` `int` `i = 0; i < len; i++) `
` ` `cout << countSmaller[i] << ` `" "` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `A[] = {12, 1, 2, 3, 0, 11, 4}; `
` ` `int` `len = ` `sizeof` `(A) / ` `sizeof` `(` `int` `); `
` ` `countSmallerRight(A, len); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

**Output:**

6 1 1 1 0 1 0

Note that the above implementation takes worst-case time complexity O(n^2) as the worst case time complexity of distance function is O(n) but the above implementation is very simple and works better than the naive algorithm in the average case.

Above approach works for unique elements but for duplicate elements just replace Set with Multiset.

## Recommended Posts:

- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count smaller elements on right side
- Count smaller elements in sorted array in C++
- Count of smaller or equal elements in sorted array
- Count the number of elements which are greater than any of element on right side of an array
- Find the farthest smaller number in the right side
- Find the nearest smaller numbers on left side in an array
- Delete array elements which are smaller than next or become smaller
- Number of elements that can be seen from right side
- Count triplets with sum smaller than a given value
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Find the element before which all the elements are smaller than it, and after which all are greater
- Minimizing array sum by subtracting larger elements from smaller ones

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.