Minimize the sum of squares of sum of N/2 paired formed by N numbers
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 = 164 Input: 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; } |
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 |
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 |
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 ?> |
164
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Recommended Posts:
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- 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
- Print the nearest prime number formed by adding prime numbers to N
- Minimize the sum of roots of a given polynomial
- Minimize (max(A[i], B[j], C[k]) - min(A[i], B[j], C[k])) of three different sorted arrays
- Minimize the maximum difference between the heights
- Minimize Cost with Replacement with other allowed
- Minimize the sum of product of two arrays with permutations allowed
- Minimize the difference between minimum and maximum elements
- Minimize cost of operation to equalize tower heights
- Minimize the maximum minimum difference after one removal from array
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.