Maximize 3 rd element sum in quadruplet sets formed from given Array | C # code to maximize 3 rd element sum in quadruplet sets formed from given Array ; Function to find the maximum possible value of Y ; Pairs contain count of minimum elements that will be utilized at place of Z . It is equal to count of possible pairs that is size of array divided by 4 ; Sorting the array in descending order so as to bring values with minimal difference closer to arr [ i ] ; Here , i + 2 acts as a pointer that points to the third value of every possible quadruplet ; Returning the optimally maximum possible value ; Driver code ; Array declaration ; Size of array
using System ; class GFG { static int formQuadruplets ( int [ ] arr , int n ) { int ans = 0 , pairs = 0 ; pairs = n / 4 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 0 ; i < n - pairs ; i += 3 ) { ans += arr [ i + 2 ] ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 } ; int n = arr . Length ; Console . Write ( formQuadruplets ( arr , n ) + " STRNEWLINE " ) ; } }