

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count smaller elements in sorted array in C++
In this tutorial, we will be discussing a program to count smaller elements in sorted array in C++.
In this we will be given a number and our task is to count all the elements present in the sorted array which are smaller than the given number.
Example
#include <bits/stdc++.h> using namespace std; int countSmaller(int arr[], int n, int x){ return upper_bound(arr, arr+n, x) - arr; } 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
- Related Questions & Answers
- Count of smaller or equal elements in the sorted array in C++
- Count elements smaller than or equal to x in a sorted matrix in C++
- Absolute distinct count in a sorted array?
- Count smaller elements on right side using Set in C++ STL
- Count of only repeated element in a sorted array of consecutive elements in C++
- Absolute distinct count in a sorted array in C++?
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- Search elements in a sorted object array in Java
- Find the Rotation Count in Rotated Sorted array in C++
- Print sorted distinct elements of array in C language
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Count 1’s in a sorted binary array in C++
- Count of Smaller Numbers After Self in C++
- Count distinct elements in an array in Python
- Count distinct elements in an array in C++
Advertisements