Minimize the sum of the squares of the sum of elements of each group the array is divided into
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 = 482 + 182 + 142 = 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++
#include <bits/stdc++.h>
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);
}
|
Java
import java.util.*;
class GFG
{
static int findAnswer( int n, int [] arr)
{
Arrays.sort(arr);
int 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;
}
public static void main(String[] args)
{
int [] arr = { 53 , 28 , 143 , 5 };
int n = arr.length;
System.out.println(findAnswer(n, arr));
}
}
|
Python3
def findAnswer(n, arr):
arr.sort(reverse = False )
sum = 0
for i in range ( int (n / 2 )):
sum + = ((arr[i] + arr[n - i - 1 ]) *
(arr[i] + arr[n - i - 1 ]))
return sum
if __name__ = = '__main__' :
arr = [ 53 , 28 , 143 , 5 ]
n = len (arr)
print (findAnswer(n, arr))
|
C#
using System;
class GFG
{
static int findAnswer( int n, int []arr)
{
Array.Sort(arr);
int 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;
}
static void Main()
{
int []arr = { 53, 28, 143, 5 };
int n = arr.Length;
Console.WriteLine(findAnswer(n, arr));
}
}
|
PHP
<?php
function findAnswer( $n , $arr )
{
sort( $arr );
$sum = 0;
for ( $i = 0; $i < $n / 2; ++ $i )
{
$sum += ( $arr [ $i ] + $arr [ $n - $i - 1]) *
( $arr [ $i ] + $arr [ $n - $i - 1]);
}
return $sum ;
}
$arr = array ( 53, 28, 143, 5);
$n = count ( $arr );
echo findAnswer( $n , $arr );
?>
|
Javascript
function findAnswer(n, arr)
{
arr.sort((a, b) => a - b);
let sum = 0;
for (let i = 0; i < Math.floor(n / 2); ++i)
{
sum += (arr[i] + arr[n - i - 1]) *
(arr[i] + arr[n - i - 1]);
}
return sum;
}
let arr = new Array( 53, 28, 143, 5);
let n = arr.length;
document.write(findAnswer(n, arr));
|
Time Complexity: O(nlogn), used for sorting the array
Auxiliary Space: O(1), as no extra space is used
Last Updated :
13 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...