Maximum absolute difference of value and index sums | Brute force PHP program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver Code
< ? php function calculateDiff ( $ i , $ j , $ arr ) { return abs ( $ arr [ $ i ] - $ arr [ $ j ] ) + abs ( $ i - $ j ) ; } function maxDistance ( $ arr , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( calculateDiff ( $ i , $ j , $ arr ) > $ result ) $ result = calculateDiff ( $ i , $ j , $ arr ) ; } } return $ result ; } $ arr = array ( -70 , -64 , -6 , -56 , 64 , 61 , -57 , 16 , 48 , -98 ) ; $ n = sizeof ( $ arr ) ; echo maxDistance ( $ arr , $ n ) ; ? >