Divide array in two Subsets such that sum of square of sum of both subsets is maximum

Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.

Examples:

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

Approach: The task is to maximize the sum of a2 + b2 where a and b are the sum of the two subsets and a + b = C (constant), i.e., the sum of the entire array. The maximum sum can be achieved by sorting the array and dividing the first N/2 – 1 smaller elements in one subset and the rest N/2 + 1 elements in the other subset. In this way, the sum can be maximized while keeping the difference in size at most 1.



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
  
#include <bits/stdc++.h>
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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
2845

Time Complexity: O(N*log(N))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.





Check out this Author's contributed articles.

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.



Improved By : AnkitRai01

Article Tags :
Practice Tags :