Maximize sum of minimum and maximum of all groups in distribution
Last Updated :
28 Jan, 2022
Given an array arr[], and an integer N. The task is to maximize the sum of minimum and maximum of each group in a distribution of the array elements in N groups where the size of each group is given in an array b[] of size N.
Examples:
Input: a[] = {17, 12, 11, 9, 8, 8, 5, 4, 3}, N = 3,
b[] = {2, 3, 4}
Output: 60
Explanation: The array elements should be distributed in groups {17, 9} {12, 8, 8} {11, 5, 4, 3}.
So the sum becomes (17 + 9) + (12 + 8) + (11 + 3) = 26 + 20 + 14 = 60
Input: a[] = {12, 3, 4, 2, 5, 9, 8, 1, 2}, N = 3,
b[] = {1, 4, 4}
Output: 45
Approach: This problem can be solved by using the Greedy Approach and some implementation. Follow the steps below to solve the given problem.
- sort array arr[] in descending order and b[] in ascending order.
- Initialize a variable ans to store the output.
- Iterate from i = 0 to i = N-1 in array a[] and add each element to ans.
- Initialize a variable ind to store the index of elements of the array arr[]. Assign N to variable ind.
- Take a loop from i=0 to N-1.
- If b[i] > 0 then increment ind with (b[i]-1)
- Add arr[ind] to ans, as arr[ind] will be the smallest element of that group
- increment ind with 1.
- output the ans.
See the illustration below for better understanding.
Illustration:
Consider an example: arr[] = {17, 12, 11, 9, 8, 8, 5, 4, 3}, N = 3 and b[] = {2, 3, 4}
Firstly, sort array arr[] in descending order and b[] in ascending order, After then put the first N greatest element of array arr[] to each group as shown in fig.
Secondly, Fill each group with the rest of the element of array arr[] (one group at a time)
Therefore answer will contain the sum of the first N elements of array arr[] i.e. 17, 12, 11 and also the last element which is filled in each group i.e. 9, 8 and 3.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int geeksforgeeks( int a[], int b[], int n, int k)
{
sort(a, a + n, greater< int >());
sort(b, b + k);
int ans = 0;
for ( int i = 0; i < k; i++) {
if (b[i] == 1) {
ans += 2 * a[i];
}
else {
ans += a[i];
}
--b[i];
}
int ind = k;
for ( int i = 0; i < k; i++) {
if (b[i] > 0) {
ind += b[i] - 1;
ans += a[ind];
ind++;
}
}
return ans;
}
int main()
{
int N = 3;
int siz = 9;
int a[] = { 17, 12, 11, 9, 8, 8, 5, 4, 3 };
int b[] = { 2, 3, 4 };
cout << geeksforgeeks(a, b, 9, N);
}
|
Java
import java.util.*;
public class GFG
{
static void sort( int arr[])
{
for ( int i = 0 ; i < arr.length; i++)
{
for ( int j = i + 1 ; j < arr.length; j++)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
static int geeksforgeeks( int a[], int b[], int n, int k)
{
sort(a);
Arrays.sort(b);
int ans = 0 ;
for ( int i = 0 ; i < k; i++) {
if (b[i] == 1 ) {
ans += 2 * a[i];
}
else {
ans += a[i];
}
--b[i];
}
int ind = k;
for ( int i = 0 ; i < k; i++) {
if (b[i] > 0 ) {
ind += b[i] - 1 ;
ans += a[ind];
ind++;
}
}
return ans;
}
public static void main(String args[])
{
int N = 3 ;
int siz = 9 ;
int a[] = { 17 , 12 , 11 , 9 , 8 , 8 , 5 , 4 , 3 };
int b[] = { 2 , 3 , 4 };
System.out.println(geeksforgeeks(a, b, 9 , N));
}
}
|
Python
def geeksforgeeks(a, b, n, k):
a.sort(reverse = True )
b.sort()
ans = 0
for i in range ( 0 , k):
if (b[i] = = 1 ):
ans = ans + ( 2 * a[i])
else :
ans = ans + a[i]
b[i] = b[i] - 1
ind = k
for i in range ( 0 , k):
if (b[i] > 0 ):
ind = ind + b[i] - 1
ans = ans + a[ind]
ind = ind + 1
return ans
N = 3
siz = 9 ;
a = [ 17 , 12 , 11 , 9 , 8 , 8 , 5 , 4 , 3 ]
b = [ 2 , 3 , 4 ]
print (geeksforgeeks(a, b, 9 , N))
|
C#
using System;
public class GFG
{
static void sort( int [] arr)
{
for ( int i = 0; i < arr.Length; i++)
{
for ( int j = i + 1; j < arr.Length; j++)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
static int geeksforgeeks( int [] a, int [] b, int n, int k)
{
sort(a);
Array.Sort(b);
int ans = 0;
for ( int i = 0; i < k; i++) {
if (b[i] == 1) {
ans += 2 * a[i];
}
else {
ans += a[i];
}
--b[i];
}
int ind = k;
for ( int i = 0; i < k; i++) {
if (b[i] > 0) {
ind += b[i] - 1;
ans += a[ind];
ind++;
}
}
return ans;
}
public static void Main()
{
int N = 3;
int siz = 9;
int [] a = { 17, 12, 11, 9, 8, 8, 5, 4, 3 };
int [] b = { 2, 3, 4 };
Console.Write(geeksforgeeks(a, b, 9, N));
}
}
|
Javascript
<script>
function geeksforgeeks(a, b, n, k)
{
a.sort( function (a, b) { return b - a })
b.sort( function (a, b) { return a - b })
let ans = 0;
for (let i = 0; i < k; i++) {
if (b[i] == 1) {
ans += 2 * a[i];
}
else {
ans += a[i];
}
--b[i];
}
let ind = k;
for (let i = 0; i < k; i++) {
if (b[i] > 0) {
ind += b[i] - 1;
ans += a[ind];
ind++;
}
}
return ans;
}
let N = 3;
let siz = 9;
let a = [17, 12, 11, 9, 8, 8, 5, 4, 3];
let b = [2, 3, 4];
document.write(geeksforgeeks(a, b, 9, N));
</script>
|
Time complexity: O(M * logM) where M is the size of array arr.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...