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 Count smaller elements on right side using Set in C++ STL K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Count smaller elements on right side using Set in C++ STL 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 element arr[i] in the array. 4 min read Count smaller elements on right side using Set in C++ STL 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 element arr[i] in the array. 4 min read Implementing Counting Sort using map in C++ Counting Sort is one of the best sorting algorithms which can sort in O(n) time complexity but the disadvantage with the counting sort is it's space complexity, for a small collection of values, it will also require a huge amount of unused space. So, we need two things to overcome this: A data struc 2 min read Implementing Counting Sort using map in C++ Counting Sort is one of the best sorting algorithms which can sort in O(n) time complexity but the disadvantage with the counting sort is it's space complexity, for a small collection of values, it will also require a huge amount of unused space. So, we need two things to overcome this: A data struc 2 min read 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 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 Like