Given two arrays arr[] and S[] consisting of N and K integers, the task is to find the maximum sum of minimum and maximum of each subset after splitting the array into K subsets such that the size of each subset is equal to one of the elements in the array S[].
Examples:
Input: arr[] = {1, 13, 7, 17}, S[] = {1, 3}
Output: 48
Explanation: Consider splitting the array as {17}, {1, 7, 13}, such that the size of subsets are 1 and 3 respectively, which is present in the array S[].
The sum of the maximum and minimum of each subset is (17 + 17 + 1 + 13) = 48.
Input: arr[ ] = {5, 1, -30, 0, 11, 20, 19}, S[] = {4, 1, 2}
Output: 45
Approach: The given problem can be solved using the Greedy Approach, the idea is to insert the first K maximum elements in each group and then start filling the smaller-sized groups first with greater elements. Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0, to store the sum of the maximum and the minimum of all the subsets.
- Sort the array arr[] in descending order.
- Sort the array S[] in ascending order.
- Find the sum of the first K elements of the array and store it in the variable ans, and decrement all elements of the S[] by 1.
- Initialize a variable, say counter as K – 1, to store the index of the minimum element of each subset.
- Traverse the array S[] and increment counter by S[i] and add the value of arr[counter] to the ans.
- After completing the above steps, print the values of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumSum( int arr[], int S[],
int N, int K)
{
int ans = 0;
sort(arr, arr + N, greater< int >());
for ( int i = 0; i < K; i++)
ans += arr[i];
sort(S, S + K);
for ( int i = 0; i < K; i++) {
if (S[i] == 1)
ans += arr[i];
S[i]--;
}
int counter = K - 1;
for ( int i = 0; i < K; i++) {
counter = counter + S[i];
if (S[i] != 0)
ans += arr[counter];
}
return ans;
}
int main()
{
int arr[] = { 1, 13, 7, 17 };
int S[] = { 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = sizeof (S) / sizeof (S[0]);
cout << maximumSum(arr, S, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int maximumSum( int arr[], int S[],
int N, int K)
{
int ans = 0 ;
Arrays.sort(arr);
for ( int i = 0 ; i < N / 2 ; i++)
{
int temp = arr[i];
arr[i] = arr[N - 1 - i];
arr[N - 1 - i] = temp;
}
for ( int i = 0 ; i < K; i++)
ans += arr[i];
Arrays.sort(S);
for ( int i = 0 ; i < K; i++)
{
if (S[i] == 1 )
ans += arr[i];
S[i]--;
}
int counter = K - 1 ;
for ( int i = 0 ; i < K; i++)
{
counter = counter + S[i];
if (S[i] != 0 )
ans += arr[counter];
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 13 , 7 , 17 };
int S[] = { 1 , 3 };
int N = arr.length;
int K = S.length;
System.out.println(maximumSum(arr, S, N, K));
}
}
|
Python3
def maximumSum(arr, S, N, K):
ans = 0
arr = sorted (arr)[:: - 1 ]
for i in range (K):
ans + = arr[i]
S = sorted (S)
for i in range (K):
if (S[i] = = 1 ):
ans + = arr[i]
S[i] - = 1
counter = K - 1
for i in range (K):
counter = counter + S[i]
if (S[i] ! = 0 ):
ans + = arr[counter]
return ans
if __name__ = = '__main__' :
arr = [ 1 , 13 , 7 , 17 ]
S = [ 1 , 3 ]
N = len (arr)
K = len (S)
print (maximumSum(arr, S, N, K))
|
C#
using System;
class GFG {
static int maximumSum( int [] arr, int [] S, int N, int K)
{
int ans = 0;
Array.Sort(arr);
for ( int i = 0; i < N / 2; i++) {
int temp = arr[i];
arr[i] = arr[N - 1 - i];
arr[N - 1 - i] = temp;
}
for ( int i = 0; i < K; i++)
ans += arr[i];
Array.Sort(S);
for ( int i = 0; i < K; i++) {
if (S[i] == 1)
ans += arr[i];
S[i]--;
}
int counter = K - 1;
for ( int i = 0; i < K; i++) {
counter = counter + S[i];
if (S[i] != 0)
ans += arr[counter];
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 1, 13, 7, 17 };
int [] S = { 1, 3 };
int N = arr.Length;
int K = S.Length;
Console.WriteLine(maximumSum(arr, S, N, K));
}
}
|
Javascript
<script>
function maximumSum(arr, S, N, K)
{
var ans = 0;
var i;
arr.sort((a, b) => b - a);
for (i = 0; i < K; i++)
ans += arr[i];
S.sort();
for (i = 0; i < K; i++) {
if (S[i] == 1)
ans += arr[i];
S[i] -= 1;
}
var counter = K - 1;
for (i = 0; i < K; i++) {
counter = counter + S[i];
if (S[i] != 0)
ans += arr[counter];
}
return ans;
}
var arr = [1, 13, 7, 17];
var S = [1, 3];
var N = arr.length;
var K = S.length;
document.write(maximumSum(arr, S, N, K));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)