给定一个包含偶数个元素的数组,任务是将数组划分为M个元素组(每个组必须包含至少2个元素),以使每个组的和的平方和最小化,即
(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
例子:
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
方法:我们的最终总和取决于两个因素:
- 每个组元素的总和。
- 所有此类组的平方和。
如果我们最小化上述两个因素,我们可以最小化结果。为了最小化第二个因素,我们应该将组的大小最小化,即只有两个元素。为了使第一因素最小化,我们可以将最小数字与最大数字配对,将第二最小数字与第二最大数字配对,依此类推。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimized sum
unsigned long long findAnswer(int n,
vector& 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 arr = { 53, 28, 143, 5 };
int n = arr.size();
cout << findAnswer(n, arr);
}
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
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
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
PHP
输出:
28465