Smallest subset of maximum sum possible by splitting array into two subsets
Last Updated :
24 May, 2021
Given an array arr[] consisting of N integers, the task is to print the smaller of the two subsets obtained by splitting the array into two subsets such that the sum of the smaller subset is maximized.
Examples:
Input: arr[] = {5, 3, 2, 4, 1, 2}
Output: 4 5
Explanation:
Split the array into two subsets as {4, 5} and {1, 2, 2, 3}.
The subset {4, 5} is of minimum length, i.e. 2, having maximum sum = 4 + 5 = 9.
Input: arr[] = {20, 15, 20, 50, 20}
Output: 15 50
Approach: The given problem can be solved by using Hashing and Sorting.
Follow the steps below to solve the problem:
- Initialize a HashMap, say M, to store the frequency of each character of the array arr[].
- Traverse the array arr[] and increment the count of every character in the HashMap M.
- Initialize 2 variables, say S, and flag, to store the sum of the first subset and to store if an answer exists or not respectively.
- Sort the array arr[] in ascending order.
- Initialize an ArrayList, say ans, to store the elements of the resultant subset.
- Traverse the array arr[] in reverse order and perform the following steps:
- Store the frequency of the current character in a variable, say F.
- If (F + ans.size()) is less than (N - (F + ans.size())) then append the element arr[i] in the ArrayList ans F number of times.
- Decrement the value of i by F.
- If the value of S is greater than the sum of the array elements, then mark the flag as true and then break.
- After completing the above steps, if the value of flag is true, then print the ArrayList ans as the resultant subset. Otherwise, print -1.the
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to split array elements
// into two subsets having sum of
// the smaller subset maximized
static void findSubset(vector<int> arr)
{
// Stores the size of the array
int N = arr.size();
// Stores the frequency
// of array elements
map<int,int> mp;
// Stores the total
// sum of the array
int totSum = 0;
// Stores the sum of
// the resultant set
int s = 0;
// Stores if it is possible
// to split the array that
// satisfies the conditions
int flag = 0;
// Stores the elements
// of the first subseta
vector<int> ans;
// Traverse the array arr[]
for (int i = 0;
i < arr.size(); i++) {
// Increment total sum
totSum += arr[i];
// Increment count of arr[i]
mp[arr[i]]=mp[arr[i]]+1;
}
// Sort the array arr[]
sort(arr.begin(),arr.end());
// Stores the index of the
// last element of the array
int i = N - 1;
// Traverse the array arr[]
while (i >= 0) {
// Stores the frequency
// of arr[i]
int frq = mp[arr[i]];
// If frq + ans.size() is
// at most remaining size
if ((frq + ans.size())
< (N - (frq + ans.size())))
{
for (int k = 0; k < frq; k++)
{
// Append arr[i] to ans
ans.push_back(arr[i]);
// Decrement totSum by arr[i]
totSum -= arr[i];
// Increment s by arr[i]
s += arr[i];
i--;
}
}
// Otherwise, decrement i
// by frq
else {
i -= frq;
}
// If s is greater
// than totSum
if (s > totSum) {
// Mark flag 1
flag = 1;
break;
}
}
// If flag is equal to 1
if (flag == 1) {
// Print the arrList ans
for (i = ans.size() - 1;
i >= 0; i--) {
cout<<ans[i]<<" ";
}
}
// Otherwise, print "-1"
else {
cout<<-1;
}
}
// Driver Code
int main()
{
vector<int> arr = { 5, 3, 2, 4, 1, 2 };
findSubset(arr);
}
// This code is contributed by mohit kumar 29.
Java
// Java program for above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to split array elements
// into two subsets having sum of
// the smaller subset maximized
static void findSubset(int[] arr)
{
// Stores the size of the array
int N = arr.length;
// Stores the frequency
// of array elements
Map<Integer, Integer> map
= new HashMap<>();
// Stores the total
// sum of the array
int totSum = 0;
// Stores the sum of
// the resultant set
int s = 0;
// Stores if it is possible
// to split the array that
// satisfies the conditions
int flag = 0;
// Stores the elements
// of the first subset
ArrayList<Integer> ans
= new ArrayList<>();
// Traverse the array arr[]
for (int i = 0;
i < arr.length; i++) {
// Increment total sum
totSum += arr[i];
// Increment count of arr[i]
map.put(arr[i],
map.getOrDefault(
arr[i], 0)
+ 1);
}
// Sort the array arr[]
Arrays.sort(arr);
// Stores the index of the
// last element of the array
int i = N - 1;
// Traverse the array arr[]
while (i >= 0) {
// Stores the frequency
// of arr[i]
int frq = map.get(arr[i]);
// If frq + ans.size() is
// at most remaining size
if ((frq + ans.size())
< (N - (frq + ans.size()))) {
for (int k = 0; k < frq; k++) {
// Append arr[i] to ans
ans.add(arr[i]);
// Decrement totSum by arr[i]
totSum -= arr[i];
// Increment s by arr[i]
s += arr[i];
i--;
}
}
// Otherwise, decrement i
// by frq
else {
i -= frq;
}
// If s is greater
// than totSum
if (s > totSum) {
// Mark flag 1
flag = 1;
break;
}
}
// If flag is equal to 1
if (flag == 1) {
// Print the arrList ans
for (i = ans.size() - 1;
i >= 0; i--) {
System.out.print(
ans.get(i) + " ");
}
}
// Otherwise, print "-1"
else {
System.out.print(-1);
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 5, 3, 2, 4, 1, 2 };
findSubset(arr);
}
}
Python3
# Python 3 program for the above approach
from collections import defaultdict
# Function to split array elements
# into two subsets having sum of
# the smaller subset maximized
def findSubset(arr):
# Stores the size of the array
N = len(arr)
# Stores the frequency
# of array elements
mp = defaultdict(int)
# Stores the total
# sum of the array
totSum = 0
# Stores the sum of
# the resultant set
s = 0
# Stores if it is possible
# to split the array that
# satisfies the conditions
flag = 0
# Stores the elements
# of the first subseta
ans = []
# Traverse the array arr[]
for i in range(len(arr)):
# Increment total sum
totSum += arr[i]
# Increment count of arr[i]
mp[arr[i]] = mp[arr[i]]+1
# Sort the array arr[]
arr.sort()
# Stores the index of the
# last element of the array
i = N - 1
# Traverse the array arr[]
while (i >= 0):
# Stores the frequency
# of arr[i]
frq = mp[arr[i]]
# If frq + ans.size() is
# at most remaining size
if ((frq + len(ans))
< (N - (frq + len(ans)))):
for k in range(frq):
# Append arr[i] to ans
ans.append(arr[i])
# Decrement totSum by arr[i]
totSum -= arr[i]
# Increment s by arr[i]
s += arr[i]
i -= 1
# Otherwise, decrement i
# by frq
else:
i -= frq
# If s is greater
# than totSum
if (s > totSum):
# Mark flag 1
flag = 1
break
# If flag is equal to 1
if (flag == 1):
# Print the arrList ans
for i in range(len(ans) - 1, -1, -1):
print(ans[i], end = " ")
# Otherwise, print "-1"
else:
print(-1)
# Driver Code
if __name__ == "__main__":
arr = [5, 3, 2, 4, 1, 2]
findSubset(arr)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to split array elements
// into two subsets having sum of
// the smaller subset maximized
static void findSubset(List<int> arr)
{
// Stores the size of the array
int N = arr.Count;
int i;
// Stores the frequency
// of array elements
Dictionary<int,int> mp = new Dictionary<int,int>();
// Stores the total
// sum of the array
int totSum = 0;
// Stores the sum of
// the resultant set
int s = 0;
// Stores if it is possible
// to split the array that
// satisfies the conditions
int flag = 0;
// Stores the elements
// of the first subseta
List<int> ans = new List<int>();
// Traverse the array arr[]
for (i = 0;
i < arr.Count; i++) {
// Increment total sum
totSum += arr[i];
// Increment count of arr[i]
if(mp.ContainsKey(arr[i]))
mp[arr[i]]=mp[arr[i]]+1;
else
mp.Add(arr[i],1);
}
// Sort the array arr[]
arr.Sort();
// Stores the index of the
// last element of the array
i = N - 1;
// Traverse the array arr[]
while (i >= 0) {
// Stores the frequency
// of arr[i]
int frq = mp[arr[i]];
// If frq + ans.size() is
// at most remaining size
if ((frq + ans.Count)
< (N - (frq + ans.Count)))
{
for (int k = 0; k < frq; k++)
{
// Append arr[i] to ans
ans.Add(arr[i]);
// Decrement totSum by arr[i]
totSum -= arr[i];
// Increment s by arr[i]
s += arr[i];
i--;
}
}
// Otherwise, decrement i
// by frq
else {
i -= frq;
}
// If s is greater
// than totSum
if (s > totSum) {
// Mark flag 1
flag = 1;
break;
}
}
// If flag is equal to 1
if (flag == 1) {
// Print the arrList ans
for (i = ans.Count - 1;
i >= 0; i--) {
Console.Write(ans[i]+" ");
}
}
// Otherwise, print "-1"
else {
Console.Write(-1);
}
}
// Driver Code
public static void Main()
{
List<int> arr = new List<int>(){ 5, 3, 2, 4, 1, 2 };
findSubset(arr);
}
}
// This code is contributed by ipg2016107.
JavaScript
<script>
// Javascript program for the above approach
// Function to split array elements
// into two subsets having sum of
// the smaller subset maximized
function findSubset(arr)
{
// Stores the size of the array
var N = arr.length;
// Stores the frequency
// of array elements
var mp = new Map();
// Stores the total
// sum of the array
var totSum = 0;
// Stores the sum of
// the resultant set
var s = 0;
// Stores if it is possible
// to split the array that
// satisfies the conditions
var flag = 0;
// Stores the elements
// of the first subseta
var ans = [];
// Traverse the array arr[]
for (var i = 0;
i < arr.length; i++) {
// Increment total sum
totSum += arr[i];
// Increment count of arr[i]
if(mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
// Sort the array arr[]
arr.sort((a,b)=> a-b)
// Stores the index of the
// last element of the array
var i = N - 1;
// Traverse the array arr[]
while (i >= 0) {
// Stores the frequency
// of arr[i]
var frq = mp.get(arr[i]);
// If frq + ans.size() is
// at most remaining size
if ((frq + ans.length)
< (N - (frq + ans.length)))
{
for (var k = 0; k < frq; k++)
{
// Append arr[i] to ans
ans.push(arr[i]);
// Decrement totSum by arr[i]
totSum -= arr[i];
// Increment s by arr[i]
s += arr[i];
i--;
}
}
// Otherwise, decrement i
// by frq
else {
i -= frq;
}
// If s is greater
// than totSum
if (s > totSum) {
// Mark flag 1
flag = 1;
break;
}
}
// If flag is equal to 1
if (flag == 1) {
// Print the arrList ans
for (i = ans.length - 1;
i >= 0; i--) {
document.write( ans[i] + " ");
}
}
// Otherwise, print "-1"
else {
document.write(-1);
}
}
// Driver Code
var arr = [5, 3, 2, 4, 1, 2 ];
findSubset(arr);
// This code is contributed by rutvik_56.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Similar Reads
Maximum possible difference of sum of two subsets of an array | Set 2 Given an array arr[ ] consisting of N integers, the task is to find maximum difference between the sum of two subsets obtained by partitioning the array into any two non-empty subsets. Note: The subsets cannot any common element. A subset can contain repeating elements. Examples: Input: arr[] = {1,
10 min read
Length of the smallest subarray with maximum possible sum Given an array arr[] consisting of N non-negative integers, the task is to find the minimum length of the subarray whose sum is maximum. Example: Input: arr[] = {0, 2, 0, 0, 12, 0, 0, 0}Output: 4Explanation: The sum of the subarray {2, 0, 0, 12} = 2 + 0 + 0 + 12 = 14, which is maximum sum possible a
6 min read
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum Given an array A containing N elements. Partition any subset of this array into two disjoint subsets such that both the subsets have an identical sum. Obtain the maximum sum that can be obtained after partitioning. Note: It is not necessary to partition the entire array, that is any element might no
15+ min read
Maximum sum of smallest and second smallest in an array Given an array arr[] of size n, the task is to find the maximum sum of the smallest and second smallest elements among all possible subarrays of size greater than equals to two.Examples:Input : arr[] = [4, 3, 1, 5, 6]Output : 11Subarrays with smallest and second smallest are,Subarray: [4, 3], smalle
8 min read
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 Pa
6 min read