
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count smaller elements on right side using Set in C++ STL
In this tutorial, we will be discussing a program to count smaller elements on right side using set in C++ STL.
For this we will be provided with an array. Our task is to construct a new array and add the number of smaller elements on the right side of the current element at its position.
Example
#include <bits/stdc++.h> using namespace std; void count_Rsmall(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] << " "; } int main(){ int A[] = {12, 1, 2, 3, 0, 11, 4}; int len = sizeof(A) / sizeof(int); count_Rsmall(A, len); return 0; }
Output
6 1 1 1 0 1 0
- Related Articles
- How to set the legends using ggplot2 on top-right side in R?
- Replace Elements with Greatest Element on Right Side in C++
- Set count() function in C++ STL
- Count smaller elements in sorted array in C++
- Number of Larger Elements on right side in a string in C++
- Count of smaller or equal elements in the sorted array in C++
- Counting Inversions using Set in C++ STL
- Find maximum difference between nearest left and right smaller elements in Python
- Find maximum difference between nearest left and right smaller elements in C++
- Count elements smaller than or equal to x in a sorted matrix in C++
- Why Indian Vehicles steering is on Left Side while few Foreign countries in right side?
- multimap::count() in C++ STL
- Program to return number of smaller elements at right of the given list in Python
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- multiset count() function in C++ STL

Advertisements