Open In App

Minimizing Array sum with groupings

Improve
Improve
Like Article
Like
Save
Share
Report

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));


Output

16



Time Complexity: O(n*logn), where n is the number of elements of the array.
Auxiliary Space: O(1)



Last Updated : 28 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads