Maximum difference between groups of size two | Javascript program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code
function CalculateMax ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let min_sum = arr [ 0 ] + arr [ 1 ] ; let max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . abs ( max_sum - min_sum ) ) ; } let arr = [ 6 , 7 , 1 , 11 ] ; let n = arr . length ; document . write ( CalculateMax ( arr , n ) ) ;