Given N numbers(N is an even number). Divide the N numbers into N/2 pairs in such a way that the sum of squares of the sum of numbers in pairs is minimal. The task is to print the minimal sum.

**Examples:**

Input:a[] = {8, 5, 2, 3}Output:164 Divide them into two groups of {2, 8} and {3, 5} to give (2+8)^{2}+ (3+5)^{2}= 164Input:a[] = {1, 1, 1, 2, 2, 2}Output:27

**Approach: ** The task is to minimize the sum of squares, hence we divide the smallest and largest number in the first group and the second smallest and second largest in the second group and so on till N/2 groups are formed. Add the numbers and store the sum of squares of them which will be the final answer.

Below is the implementation of the above approach:

## C++

`// C++ program to minimize the sum ` `// of squares of sum of numbers ` `// of N/2 groups of N numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the minimize sum ` `// of square of sum of numbers of every group ` `int` `findMinimal(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Sort the array elements ` ` ` `sort(a, a + n); ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// Iterate for the first half of array ` ` ` `for` `(` `int` `i = 0; i < n / 2; i++) ` ` ` `sum += (a[i] + a[n - i - 1]) ` ` ` `* (a[i] + a[n - i - 1]); ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 8, 5, 2, 3 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << findMinimal(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to minimize the sum ` `// of squares of sum of numbers ` `// of N/2 groups of N numbers ` `import` `java.util.Arrays; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns the minimize sum ` ` ` `// of square of sum of numbers of every group ` ` ` `static` `int` `findMinimal(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `// Sort the array elements ` ` ` `Arrays.sort(a); ` ` ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Iterate for the first half of array ` ` ` `for` `(` `int` `i = ` `0` `; i < n / ` `2` `; i++) ` ` ` `sum += (a[i] + a[n - i - ` `1` `]) * ` ` ` `(a[i] + a[n - i - ` `1` `]); ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String str[]) ` ` ` `{ ` ` ` `int` `[]a = { ` `8` `, ` `5` `, ` `2` `, ` `3` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(findMinimal(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to minimize the sum ` `# of squares of sum of numbers ` `# of N/2 groups of N numbers ` ` ` `# Function that returns the minimize sum ` `# of square of sum of numbers of every group ` `def` `findMinimal(a, n): ` ` ` ` ` `# Sort the array elements ` ` ` `a.sort() ` ` ` ` ` `sum` `=` `0` ` ` ` ` `# Iterate for the first half of array ` ` ` `for` `i ` `in` `range` `( n ` `/` `/` `2` `): ` ` ` `sum` `+` `=` `((a[i] ` `+` `a[n ` `-` `i ` `-` `1` `]) ` `*` ` ` `(a[i] ` `+` `a[n ` `-` `i ` `-` `1` `])) ` ` ` ` ` `return` `sum` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `8` `, ` `5` `, ` `2` `, ` `3` `] ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `print` `(findMinimal(a, n)) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# program to minimize the sum ` `// of squares of sum of numbers ` `// of N/2 groups of N numbers ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns the minimize sum ` `// of square of sum of numbers of every group ` `static` `int` `findMinimal(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `// Sort the array elements ` ` ` `Array.Sort(a); ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// Iterate for the first half of array ` ` ` `for` `(` `int` `i = 0; i < n / 2; i++) ` ` ` `sum += (a[i] + a[n - i - 1]) * ` ` ` `(a[i] + a[n - i - 1]); ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]a = { 8, 5, 2, 3 }; ` ` ` `int` `n = a.Length; ` ` ` ` ` `Console.Write(findMinimal(a, n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to minimize the sum ` `// of squares of sum of numbers ` `// of N/2 groups of N numbers ` ` ` `// Function that returns the minimize sum ` `// of square of sum of numbers of every group ` `function` `findMinimal(` `$a` `, ` `$n` `) ` `{ ` ` ` `// Sort the array elements ` ` ` `sort(` `$a` `); ` ` ` ` ` `$sum` `= 0; ` ` ` ` ` `// Iterate for the first half of array ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `/ 2; ` `$i` `++) ` ` ` `$sum` `+= (` `$a` `[` `$i` `] + ` `$a` `[` `$n` `- ` `$i` `- 1]) * ` ` ` `(` `$a` `[` `$i` `] + ` `$a` `[` `$n` `- ` `$i` `- 1]); ` ` ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$a` `= ` `array` `(8, 5, 2, 3 ); ` `$n` `= sizeof(` `$a` `); ` ` ` `echo` `findMinimal(` `$a` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

164

**Time Complexity:** O(N log N)

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Count subarrays with sum as difference of squares of two numbers
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Minimize sum of prime numbers added to make an array non-decreasing
- Minimum sum of two numbers formed from digits of an array
- Sum of all numbers that can be formed with permutations of n digits
- Minimum sum of two numbers formed from digits of an array
- Count numbers formed by given two digit with sum having given digits
- Minimum sum of two numbers formed from digits of an array in O(n)
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Check if the sum of perfect squares in an array is divisible by x
- Sum of squares of all Subsets of given Array
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Print the nearest prime number formed by adding prime numbers to N
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.