Count array elements with rank not exceeding K | Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Driver Code ; Given array ; Given K ; Function Call
def rankLessThanK ( arr , k , n ) : NEW_LINE INDENT rank = 1 ; NEW_LINE position = 1 ; NEW_LINE arr = sorted ( arr ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i == n - 1 or arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT rank = position ; NEW_LINE if ( rank > k ) : NEW_LINE INDENT return position - 1 ; NEW_LINE DEDENT DEDENT position += 1 ; NEW_LINE DEDENT return n ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 4 ; NEW_LINE print ( rankLessThanK ( arr , K , N ) ) ; NEW_LINE DEDENT