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