Count elements such that there are exactly X elements with values greater than or equal to X | Python3 implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Driver codemain ( )
from bisect import bisect as upper_bound NEW_LINE def getCount ( v , n ) : NEW_LINE INDENT v = sorted ( v ) NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp = n - 1 - upper_bound ( v , v [ i ] - 1 ) NEW_LINE if ( tmp == v [ i ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT n = 4 NEW_LINE v = [ ] NEW_LINE v . append ( 1 ) NEW_LINE v . append ( 2 ) NEW_LINE v . append ( 3 ) NEW_LINE v . append ( 4 ) NEW_LINE print ( getCount ( v , n ) ) NEW_LINE