给定一个整数数组arr[] ,任务是将该数组划分为两个非空子集,使得两个子集之和的平方和最大,并且两个子集的大小相差不得超过 1 .
例子:
Input: arr[] = {1, 2, 3}
Output: 26
Explanation:
Sum of Subset Pairs are as follows
(1)2 + (2 + 3)2 = 26
(2)2 + (1 + 3)2 = 20
(3)2 + (1 + 2)2 = 18
Maximum among these is 26, Therefore the required sum is 26
Input: arr[] = {7, 2, 13, 4, 25, 8}
Output: 2845
方法:任务是最大化a 2 + b 2的总和,其中a和b是两个子集的总和, a + b = C (常数),即整个数组的总和。最大和可以通过对数组进行排序并将前N/2 – 1 个较小元素划分为一个子集中的元素,并将其余N/2 + 1 个元素划分为另一个子集中的元素来实现。通过这种方式,可以在保持大小差异最多为 1 的情况下最大化和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum sum of the
// square of the sum of two subsets of an array
int maxSquareSubsetSum(int* A, int N)
{
// Initialize variables to store
// the sum of subsets
int sub1 = 0, sub2 = 0;
// Sorting the array
sort(A, A + N);
// Loop through the array
for (int i = 0; i < N; i++) {
// Sum of the first subset
if (i < (N / 2) - 1)
sub1 += A[i];
// Sum of the second subset
else
sub2 += A[i];
}
// Return the maximum sum of
// the square of the sum of subsets
return sub1 * sub1 + sub2 * sub2;
}
// Driver code
int main()
{
int arr[] = { 7, 2, 13, 4, 25, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxSquareSubsetSum(arr, N);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the maximum sum of the
// square of the sum of two subsets of an array
static int maxSquareSubsetSum(int []A, int N)
{
// Initialize variables to store
// the sum of subsets
int sub1 = 0, sub2 = 0;
// Sorting the array
Arrays.sort(A);
// Loop through the array
for (int i = 0; i < N; i++)
{
// Sum of the first subset
if (i < (N / 2) - 1)
sub1 += A[i];
// Sum of the second subset
else
sub2 += A[i];
}
// Return the maximum sum of
// the square of the sum of subsets
return sub1 * sub1 + sub2 * sub2;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 7, 2, 13, 4, 25, 8 };
int N = arr.length;
System.out.println(maxSquareSubsetSum(arr, N));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the maximum sum of the
# square of the sum of two subsets of an array
def maxSquareSubsetSum(A, N) :
# Initialize variables to store
# the sum of subsets
sub1 = 0; sub2 = 0;
# Sorting the array
A.sort();
# Loop through the array
for i in range(N) :
# Sum of the first subset
if (i < (N // 2) - 1) :
sub1 += A[i];
# Sum of the second subset
else :
sub2 += A[i];
# Return the maximum sum of
# the square of the sum of subsets
return sub1 * sub1 + sub2 * sub2;
# Driver code
if __name__ == "__main__" :
arr = [ 7, 2, 13, 4, 25, 8 ];
N = len(arr);
print(maxSquareSubsetSum(arr, N));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum sum of the
// square of the sum of two subsets of an array
static int maxSquareSubsetSum(int []A, int N)
{
// Initialize variables to store
// the sum of subsets
int sub1 = 0, sub2 = 0;
// Sorting the array
Array.Sort(A);
// Loop through the array
for (int i = 0; i < N; i++)
{
// Sum of the first subset
if (i < (N / 2) - 1)
sub1 += A[i];
// Sum of the second subset
else
sub2 += A[i];
}
// Return the maximum sum of
// the square of the sum of subsets
return sub1 * sub1 + sub2 * sub2;
}
// Driver code
public static void Main()
{
int []arr = { 7, 2, 13, 4, 25, 8 };
int N = arr.Length;
Console.WriteLine(maxSquareSubsetSum(arr, N));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
2845
时间复杂度: O(N*log(N))
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live