Count elements such that there are exactly X elements with values greater than or equal to X | C ++ implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll int getCount ( vector < ll int > v , int n ) { sort ( ( v ) . begin ( ) , ( v ) . end ( ) ) ; ll int cnt = 0 ; for ( ll int i = 0 ; i < n ; i ++ ) { ll int tmp = v . end ( ) - 1 - upper_bound ( ( v ) . begin ( ) , ( v ) . end ( ) , v [ i ] - 1 ) ; if ( tmp == v [ i ] ) cnt ++ ; } return cnt ; } int main ( ) { ll int n ; n = 4 ; vector < ll int > v ; v . push_back ( 1 ) ; v . push_back ( 2 ) ; v . push_back ( 3 ) ; v . push_back ( 4 ) ; cout << getCount ( v , n ) ; return 0 ; }