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
function probability ( arr1 , arr2 ) { let N = arr1 . length ; let M = arr2 . length ; let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let y = 0 ; for ( let j = 0 ; j < M ; j ++ ) { if ( arr2 [ j ] > arr1 [ i ] ) y ++ ; } res += y ; } res = ( res / ( N * M ) ) ; return res ; } let arr1 = [ 5 , 2 , 6 , 1 ] ; let arr2 = [ 1 , 6 , 10 , 1 ] ; document . write ( probability ( arr1 , arr2 ) ) ;