Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | Python3 program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Prthe result ; Given array , arr [ ] ; Store the size of the array
from bisect import bisect_right NEW_LINE def countPairs ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT high = bisect_right ( arr , k * arr [ i ] ) NEW_LINE ans += high - i - 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 2 , 3 , 9 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE countPairs ( arr , n , k ) NEW_LINE