Minimize the sum of the squares of the sum of elements of each group the array is divided into | C ++ implementation of the approach ; Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long findAnswer ( int n , vector < int > & arr ) { sort ( arr . begin ( ) , arr . end ( ) ) ; unsigned long long sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } int main ( ) { std :: vector < int > arr = { 53 , 28 , 143 , 5 } ; int n = arr . size ( ) ; cout << findAnswer ( n , arr ) ; }