Count smaller elements in sorted array in C++ Last Updated : 30 Mar, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a sorted array and a number x, count smaller elements than x in the given array. Examples: Input : arr[] = {10, 20, 30, 40, 50} x = 45 Output : 4 There are 4 elements smaller than 45. Input : arr[] = {10, 20, 30, 40, 50} x = 40 Output : 3 There are 3 elements smaller than 40. We can use upper_bound() in C++ to quickly find the result. It returns iterator (or pointer) to first element which is greater than given number. If all elements smaller, then it returns size of array. If all elements are greater than it returns 0. CPP // CPP program to count smaller elements // in an array. #include <bits/stdc++.h> using namespace std; int countSmaller(int arr[], int n, int x) { return upper_bound(arr, arr+n, x) - arr; } // Driver code int main() { int arr[] = { 10, 20, 30, 40, 50 }; int n = sizeof(arr)/sizeof(arr[0]); cout << countSmaller(arr, n, 45) << endl; cout << countSmaller(arr, n, 55) << endl; cout << countSmaller(arr, n, 4) << endl; return 0; } Output: 4 5 0 Time Complexity : O(Log n) Comment More infoAdvertise with us Next Article Find the Frequency of Each Element in a Vector in C++ K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Find the Frequency of Each Element in a Vector in C++ The frequency of an element is number of times it occurred in the vector. In this article, we will learn different methods to find the frequency of each element in a vector in C++.The simplest method to find the frequency of each element in a vector is by iterating the vector and storing the count o 3 min read Counting Inversions using Set in C++ STL Inversion Count for an array indicates â how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum. Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. For 3 min read STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read unordered_set size() function in C++ STL The unordered_set::size() method is a builtin function in C++ STL which is used to return the number of elements in the unordered_set container. Syntax: unordered_set_name.size() Parameter: It does not accepts any parameter. Return Value: The function returns the number of elements in the container. 1 min read How to sort an Array using STL in C++? Given an array arr[], sort this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: {1, 12, 45, 54, 71, 76} Input: arr[] = {1, 7, 5, 4, 6, 12} Output: {1, 4, 5, 6, 7, 12} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax: sort(arr, arr 1 min read count() in C++ STL In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++.Letâs take a quick look at a simple example that uses count() method:C 3 min read Like