Find pair with maximum difference in any column of a Matrix | Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; driver code
< ? php function colMaxDiff ( $ mat ) { $ N = 5 ; $ max_diff = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ max_val = $ mat [ 0 ] [ $ i ] ; $ min_val = $ mat [ 0 ] [ $ i ] ; for ( $ j = 1 ; $ j < $ N ; $ j ++ ) { $ max_val = max ( $ max_val , $ mat [ $ j ] [ $ i ] ) ; $ min_val = min ( $ min_val , $ mat [ $ j ] [ $ i ] ) ; } $ max_diff = max ( $ max_diff , $ max_val - $ min_val ) ; } return $ max_diff ; } $ mat = array ( array ( 1 , 2 , 3 , 4 , 5 ) , array ( 5 , 3 , 5 , 4 , 0 ) , array ( 5 , 6 , 7 , 8 , 9 ) , array ( 0 , 6 , 3 , 4 , 12 ) , array ( 9 , 7 , 12 , 4 , 3 ) ) ; echo " Max ▁ difference ▁ : ▁ " . colMaxDiff ( $ mat ) . " STRNEWLINE " ;