Given an array consisting of **even** number of elements, the task is to divide the array into **M** group of elements (every group must contain at least 2 elements) such that the sum of the squares of the sums of each group is minimized i.e.,

(sum_of_elements_of_group1)^{2} + (sum_of_elements_of_group2)^{2} + (sum_of_elements_of_group3)^{2} + (sum_of_elements_of_group4)^{2} + ….. + (sum_of_elements_of_groupM)^{2}

**Examples:**

Input:arr[] = {5, 8, 13, 45, 6, 3}

Output:2824

Groups can be (3, 45), (5, 13) and (6, 8)

(3 + 45)^{2}+ (5 + 13)^{2}+ (6 + 8)^{2}= 48^{2}+ 18^{2}+ 14^{2}= 2304 + 324 + 196 = 2824

Input:arr[] = {53, 28, 143, 5}

Output:28465

**Approach:** Our final sum depends on two factors:

- Sum of the elements of each group.
- The sum of squares of all such groups.

If we minimize both the factors mentioned above, we can minimize the result. To minimize the second factor we should make groups of minimum size i.e. just two elements. To minimize first factor we can pair smallest number with largest number, second smallest number to second largest number and so on.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimized sum ` ` ` `static` `int` `findAnswer(` `int` `n, ` `int` `[] arr) ` ` ` `{ ` ` ` ` ` `// Sort the array to pair the elements ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Variable to hold the answer ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Pair smallest with largest, second ` ` ` `// smallest with second largest, and ` ` ` `// so on ` ` ` `for` `(` `int` `i = ` `0` `; i < n / ` `2` `; ++i) ` ` ` `{ ` ` ` `sum += (arr[i] + arr[n - i - ` `1` `]) ` ` ` `* (arr[i] + arr[n - i - ` `1` `]); ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = {` `53` `, ` `28` `, ` `143` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(findAnswer(n, arr)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the minimized sum ` `def` `findAnswer(n, arr): ` ` ` ` ` `# Sort the array to pair the elements ` ` ` `arr.sort(reverse ` `=` `False` `) ` ` ` ` ` `# Variable to hold the answer ` ` ` `sum` `=` `0` ` ` ` ` `# Pair smallest with largest, second ` ` ` `# smallest with second largest, and ` ` ` `# so on ` ` ` `for` `i ` `in` `range` `(` `int` `(n ` `/` `2` `)): ` ` ` `sum` `+` `=` `((arr[i] ` `+` `arr[n ` `-` `i ` `-` `1` `]) ` `*` ` ` `(arr[i] ` `+` `arr[n ` `-` `i ` `-` `1` `])) ` ` ` ` ` `return` `sum` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `53` `, ` `28` `, ` `143` `, ` `5` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(findAnswer(n, arr)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimized sum ` `static` `int` `findAnswer(` `int` `n, ` `int` `[]arr) ` `{ ` ` ` ` ` `// Sort the array to pair the elements ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Variable to hold the answer ` ` ` `int` `sum = 0; ` ` ` ` ` `// Pair smallest with largest, second ` ` ` `// smallest with second largest, and ` ` ` `// so on ` ` ` `for` `(` `int` `i = 0; i < n / 2; ++i) ` ` ` `{ ` ` ` `sum += (arr[i] + arr[n - i - 1]) ` ` ` `* (arr[i] + arr[n - i - 1]); ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 53, 28, 143, 5 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(findAnswer(n, arr)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimized sum ` `function` `findAnswer(` `$n` `, ` `$arr` `) ` `{ ` ` ` ` ` `// Sort the array to pair the elements ` ` ` `sort(` `$arr` `); ` ` ` ` ` `// Variable to hold the answer ` ` ` `$sum` `= 0; ` ` ` ` ` `// Pair smallest with largest, second ` ` ` `// smallest with second largest, and ` ` ` `// so on ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `/ 2; ++` `$i` `) ` ` ` `{ ` ` ` `$sum` `+= (` `$arr` `[` `$i` `] + ` `$arr` `[` `$n` `- ` `$i` `- 1]) * ` ` ` `(` `$arr` `[` `$i` `] + ` `$arr` `[` `$n` `- ` `$i` `- 1]); ` ` ` `} ` ` ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 53, 28, 143, 5); ` `$n` `= ` `count` `(` `$arr` `); ` `echo` `findAnswer(` `$n` `, ` `$arr` `); ` ` ` `// This code is contributed by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

28465

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:

- Check if N can be divided into K consecutive elements with a sum equal to N
- Maximize array sum by X increments when each element is divided by 10
- Check if an array can be divided into pairs whose sum is divisible by k
- Find if array can be divided into two subarrays of equal sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Minimize the sum of squares of sum of N/2 paired formed by N numbers
- Form an array of distinct elements with each element as sum of an element from each array
- Count of Array elements to be divided by 2 to make at least K elements equal
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Minimize increment/decrement of Array elements to make each modulo K equal
- Ways of dividing a group into two halves such that two elements are in different groups
- First N natural can be divided into two sets with given difference and co-prime sums
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Number of ways N can be divided into four parts to construct a rectangle
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Minimize operations required to make each element of Array equal to it's index value
- Minimize count of increments of each element of subarrays required to make Array non-increasing
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray

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.