Minimizing Array sum with groupings
Last Updated :
28 Aug, 2023
Given an array arr[] of integers, the task is to minimize its sum by creating groups of up to size k. Each group consists of k elements selected from the array such that the sum of the group is k times the minimum value among the chosen elements (k*min(arr1, arr2, ___, arrk), where arr1, arr2___, arrk are the k elements which you have chosen in your group). You can create an arbitrary number of groups, and the group size can be less than or equal to k. Your task is to find the maximum possible reduction in the sum of the array elements after creating such groups.
Examples:
Input: n = 3, k = 2, arr[] = {4, 4, 8}
Output: 4
Explanation: You can make a group of size 2 - index(1, 2) then initially sum was 4 + 8 = 12 but after the group sum becomes 4 + 4 = 8, So you have decreased 4 from the actual sum of these two elements and you can not group other elements to decrease overall sum further. So it will remain the same.
Input: n = 2, k = 1, arr[] = {1427, 110}
Output: 0
Explanation: The group size is 1 so you can not group any 2 or more elements, So the sum will remain the same..
Approach: To solve the problem follow the below idea:
As we have to decrease the maximum sum so we will use the Greedy approach here and will group the highest numbers with the lowest numbers so that sum will decrease maximum. We will do the same process until all the element is grouped. And to get the elements in order so that we can group efficiently we will use sorting.
Illustration:
Let's discuss a small example: arr[] = {6, 1, 10, 2, 4}, k = 3.
Now we will sort the array -> {1, 2, 4, 6, 10} and after this, we will group the maximum with the minimum like this we will make a group of {1, 6, 10} so that we can decrease the sum by (10-1+6-1) = 14 from there, and another group we will make of {2, 4} and we will decrease the sum by (4-2 = 2) from there so in this way we will decrease the sum by 16 in total.
Steps that were to follow the above approach:
- Sort the initial array
- Check for the edge case of k is 0 or 1, as in this case, we cannot reduce the overall sum further.
- For every extremely opposite element of this array,
- If the current group size is less than k, add up the difference between it and the current smallest element in the group.
- If the group becomes full, create another group.
- And, change the current minimum element as well.
- Finally, return the ans, which stores the maximum possible decrease in the overall sum of the array.
Below is the code to implement the above steps:
C++
// C++ code for the above approach.
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum
// decrement in the original sum.
int maxDecr(int n, vector<int>& arr, int k)
{
// Sorting the array
sort(arr.begin(), arr.end());
// If k size is 0 or 1
if (k == 0 or k == 1)
return 0;
// ans to store saved amount,
// j pointer which will point at the
// minimum element ofgroup size will
// tell the current group size
int ans = 0, j = 0, size = 1, i = n - 1;
// Iterating
while (i > j) {
// If group can add more elements
if (size != k) {
ans += (arr[i] - arr[j]);
size++;
}
// If group full then creating
// another group
else {
j++;
i++;
size = 1;
}
i--;
}
return ans;
}
// Driver's code
int main()
{
// Size of array and maximum
// size of group
int n = 5, k = 3;
// N Elements
vector<int> arr = { 6, 1, 10, 2, 4 };
// Function Call
cout << maxDecr(n, arr, k) << endl;
return 0;
}
Java
//Java implementation
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class GFG {
// Function to return the maximum decrement in the original sum.
static int maxDecr(int n, List<Integer> arr, int k) {
// Sorting the array
Collections.sort(arr);
// If k size is 0 or 1
if (k == 0 || k == 1)
return 0;
// ans to store saved amount,
// j pointer which will point at the
// minimum element of group size will
// tell the current group size
int ans = 0, j = 0, size = 1, i = n - 1;
// Iterating
while (i > j) {
// If group can add more elements
if (size != k) {
ans += (arr.get(i) - arr.get(j));
size++;
}
// If group full then creating another group
else {
j++;
i++;
size = 1;
}
i--;
}
return ans;
}
// Driver's code
public static void main(String[] args) {
// Size of array and maximum size of group
int n = 5, k = 3;
// N Elements
List<Integer> arr = Arrays.asList(6, 1, 10, 2, 4);
// Function Call
System.out.println(maxDecr(n, arr, k));
}
}
//This code is contributed by Aditi Tyagi
Python3
# Python code for the above approach.
import sys
# Function to return the maximum
# decrement in the original sum.
def maxDecr(n, arr, k):
# Sorting the array
arr.sort()
# If k size is 0 or 1
if k == 0 or k == 1:
return 0
# ans to store saved amount,
# j pointer which will point at the
# minimum element ofgroup size will
# tell the current group size
ans = 0
j = 0
size = 1
i = n - 1
# Iterating
while i > j:
# If group can add more elements
if size != k:
ans += (arr[i] - arr[j])
size += 1
# If group full then creating
# another group
else:
j += 1
i += 1
size = 1
i -= 1
return ans
# Driver's code
if __name__ == "__main__":
# Size of array and maximum
# size of group
n = 5
k = 3
# N Elements
arr = [6, 1, 10, 2, 4]
# Function Call
print(maxDecr(n, arr, k))
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
// Function to return the maximum
// decrement in the original sum.
static int maxDecr(int n, List<int> arr, int k)
{
// Sorting the array
arr.Sort();
// If k size is 0 or 1
if (k == 0 || k == 1)
return 0;
// ans to store saved amount,
// j pointer which will point at the
// minimum element ofgroup size will
// tell the current group size
int ans = 0, j = 0, size = 1, i = n - 1;
// Iterating
while (i > j)
{
// If group can add more elements
if (size != k)
{
ans += (arr[i] - arr[j]);
size++;
}
// If group full then creating
// another group
else
{
j++;
i++;
size = 1;
}
i--;
}
return ans;
}
// Driver code
static void Main(string[] args)
{
int n = 5, k = 3;
List<int> arr = new List<int> { 6, 1, 10, 2, 4 };
Console.WriteLine(maxDecr(n, arr, k));
}
}
JavaScript
// Function to return the maximum decrement in the original sum.
function maxDecr(n, arr, k) {
// Sorting the array
arr.sort(function(a, b) { return a - b; });
// If k size is 0 or 1
if (k === 0 || k === 1) {
return 0;
}
// ans to store saved amount,
// j pointer which will point at the
// minimum element ofgroup size will
// tell the current group size
let ans = 0, j = 0, size = 1, i = n - 1;
// Iterating
while (i > j) {
// If group can add more elements
if (size !== k) {
ans += (arr[i] - arr[j]);
size++;
}
// If group full then creating another group
else {
j++;
i++;
size = 1;
}
i--;
}
return ans;
}
// Driver's code
let n = 5, k = 3;
let arr = [6, 1, 10, 2, 4];
console.log(maxDecr(n, arr, k));
Time Complexity: O(n*logn), where n is the number of elements of the array.
Auxiliary Space: O(1)
Similar Reads
Minimum chairs for group timings Given an integer N representing the total size of groups S, their entry T1 and exit timings T2. This means that group number i will come at T1[i], leave at T2[i], and have a variable size in group S[i]. the task is to find the minimum number of chairs to be bought so that every customer has a chair
6 min read
Minimize the cost of partitioning an array into K groups Given an array arr[] and an integer K, the task is to partition the array into K non-empty groups where each group is a subarray of the given array and each element of the array is part of only one group. All the elements in a given group must have the same value. You can perform the following opera
15+ min read
Minimum total sum from the given two arrays Given two arrays A[] and B[] of N positive integers and a cost C. We can choose any one element from each index of the given arrays i.e., for any index i we can choose only element A[i] or B[i]. The task is to find the minimum total sum of selecting N elements from the given two arrays and if we are
11 min read
Minimize transfer operations to get the given Array sum Given two integer arrays A[] and B[] of length N and M respectively, the task is to find the minimum number of operations required such that the sum of elements in array A becomes targetSum. In one operation you can transfer an element from A[] to B[] or from B[] to A[]. Examples Input: N = 4, M = 3
15+ min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Maximize the Sum of Minimum in each Group of size K Given an array nums[] of size N and a positive integer K, divide the elements of the array into N/K groups, each of size K such that the sum of the smallest elements in each group is maximized. Examples: Input: nums = {1,4,3,2}, K = 2Output: 4Explanation: All possible groupings (ignoring the orderin
8 min read
Construct sum-array with sum of elements in given range You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
7 min read
Minimizing Sum with Neighbor Value Priority in Array Transformation Given an array arr[] of length n, where arr[i] represents some value. You have to create an array result[] of length n, such that it follows both conditions given below and the task is to find the minimum sum of the result[] while ensuring that these rules are satisfied. result[i] >= 1, for all i
8 min read
Divide 1 to n into two groups with minimum sum difference Given a positive integer n, such that n > 2. Your task is to divide the numbers from 1 to n in two groups, such that the absolute difference between the sum of the elements of the these two groups is minimum possible.Examples:Input: n = 5Output: [ [ 2, 5 ], [ 1, 3, 4 ] ]Explanation: The sum of fi
12 min read
Find the Sub-array with sum closest to 0 Given an array of both positive and negative numbers, the task is to find out the subarray whose sum is closest to 0. There can be multiple such subarrays, we need to output just 1 of them. Examples: Input : arr[] = {-1, 3, 2, -5, 4} Output : 1, 3 Subarray from index 1 to 3 has sum closest to 0 i.e.
13 min read