Minimum difference between groups of size two | C # program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Generating sum groups . ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static long calculate ( long [ ] a , int n ) { Array . Sort ( a ) ; int i , j ; List < long > s = new List < long > ( ) ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . Add ( ( a [ i ] + a [ j ] ) ) ; long mini = s . Min ( ) ; long maxi = s . Max ( ) ; return Math . Abs ( maxi - mini ) ; } public static void Main ( ) { long [ ] a = { 2 , 6 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( calculate ( a , n ) ) ; } }