Maximize sum of consecutive differences in a circular array | Return the maximum Sum of difference between consecutive elements . ; Sorting the array . ; Subtracting a1 , a2 , a3 , ... . . , a ( n / 2 ) - 1 , an / 2 twice and adding a ( n / 2 ) + 1 , a ( n / 2 ) + 2 , a ( n / 2 ) + 3 , ... . . , an - 1 , an twice . ; Driver Code
function maxSum ( arr , n ) { let sum = 0 ; arr . sort ( ) ; for ( let i = 0 ; i < n / 2 ; i ++ ) { sum -= ( 2 * arr [ i ] ) ; sum += ( 2 * arr [ n - i - 1 ] ) ; } return sum ; } let arr = [ 4 , 2 , 1 , 8 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;