Utility method to compute cost , when all values of array are made equal to X
< ? php function computeCost ( $ arr , $ N , $ X ) { $ cost = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ cost += abs ( $ arr [ $ i ] - $ X ) ; return $ cost ; }