Count ways of choosing a pair with maximum difference | PHP Code to find no . of Ways of choosing a pair with maximum difference ; To find minimum and maximum of the array ; to find the count of minimum and maximum elements ; Count variables ; condition for all elements equal ; Driver code
< ? php function countPairs ( $ a , $ n ) { $ mn = PHP_INT_MAX ; $ mx = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ mn = min ( $ mn , $ a [ $ i ] ) ; $ mx = max ( $ mx , $ a [ $ i ] ) ; } $ c1 = 0 ; $ c2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ mn ) $ c1 ++ ; if ( $ a [ $ i ] == $ mx ) $ c2 ++ ; } if ( $ mn == $ mx ) return $ n * ( $ n - 1 ) / 2 ; else return $ c1 * $ c2 ; } $ a = array ( 3 , 2 , 1 , 1 , 3 ) ; $ n = count ( $ a ) ; echo countPairs ( $ a , $ n ) ; ? >