Count of distinct index pair ( i , j ) such that element sum of First Array is greater | Python3 program of the above approach ; Function to find the number of pairs . ; Array c [ ] where c [ i ] = a [ i ] - b [ i ] ; Sort the array c ; Initialise answer as 0 ; Iterate from index 0 to n - 1 ; If c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i ; Find the minimum index such that c [ i ] + c [ j ] > 0 which is equivalent to c [ j ] >= - c [ i ] + 1 ; Add ( i - pos ) to answer ; Return the answer ; Driver code ; Number of elements in a and b ; Array a ; Array b
from bisect import bisect_left NEW_LINE def numberOfPairs ( a , b , n ) : NEW_LINE INDENT c = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = a [ i ] - b [ i ] NEW_LINE DEDENT c = sorted ( c ) NEW_LINE answer = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( c [ i ] <= 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT pos = bisect_left ( c , - c [ i ] + 1 ) NEW_LINE answer += ( i - pos ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE b = [ 2 , 5 , 6 , 1 , 9 ] NEW_LINE print ( numberOfPairs ( a , b , n ) ) NEW_LINE DEDENT