Probability of a random pair being the maximum weighted pair | Function to return probability ; Count occurrences of maximum element in A [ ] ; Count occurrences of maximum element in B [ ] ; Returning probability ; Driver Code
function probability ( a , b , size1 , size2 ) { let max1 = Number . MIN_VALUE , count1 = 0 ; for ( let i = 0 ; i < size1 ; i ++ ) { if ( a [ i ] > max1 ) { max1 = a [ i ] ; count1 = 1 ; } else if ( a [ i ] == max1 ) { count1 ++ ; } } let max2 = Number . MIN_VALUE , count2 = 0 ; for ( let i = 0 ; i < size2 ; i ++ ) { if ( b [ i ] > max2 ) { max2 = b [ i ] ; count2 = 1 ; } else if ( b [ i ] == max2 ) { count2 ++ ; } } return ( count1 * count2 ) / ( size1 * size2 ) ; } let a = [ 1 , 2 , 3 ] ; let b = [ 1 , 3 , 3 ] ; let size1 = a . length ; let size2 = b . length ; document . write ( probability ( a , b , size1 , size2 ) ) ;