Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | Function to find probability such that x < y and X belongs to arr1 [ ] and Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Traverse the arr2 [ ] ; If arr2 [ j ] is greater than arr1 [ i ] ; Increment res by y ; Update the value of res ; Return resultant probability ; Driver Code
def probability ( arr1 , arr2 ) : NEW_LINE INDENT N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT y = 0 NEW_LINE for j in range ( M ) : NEW_LINE INDENT if ( arr2 [ j ] > arr1 [ i ] ) : NEW_LINE INDENT y += 1 NEW_LINE DEDENT DEDENT res += y NEW_LINE DEDENT res = res / ( N * M ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 5 , 2 , 6 , 1 ] NEW_LINE arr2 = [ 1 , 6 , 10 , 1 ] NEW_LINE print ( probability ( arr1 , arr2 ) ) NEW_LINE DEDENT