Minimize cost by splitting the given Array into subsets of size K and adding the highest K/2 elements of each subset into the cost


Splitting the array means we have to divide the array and make subsets. Here in this problem, we have given an array of integers with size n and integer k and our goal is to calculate the lowest cost by splitting the whole given array into the subsets of size k and adding the highest k/2 element of each subset into the cost.

NOTE: here we consider a ceiling of k/2.

Let’s see examples with explanations below to understand the problem in a better way.

Sample Example

Input

n: 4
array: [ 3, 4, 2, 1 ]
k: 2

Output

6

Explanation: Here we split the given array as [ 3, 4 ] and [ 2, 1 ]

And now we have to add the highest k/2 element of each subset into the cost.

k/2 = 2/2 = 1.

Cost = 4 + 2 = 6

Input

n: 10
array: [ 3, 5, 2, 6, 7, 4, 1, 8, 11, 10 ]
k: 5

Output

41

Explanation: Here we split the given array as [ 3, 5, 2, 4, 1 ] and [ 6, 7, 8, 11, 10 ]

And now we have to add the highest k/2 element of each subset into the cost.

k/2 = 5/2 = 2.5 now ceiling of [2.5] = 3.

We have to add 3 highest elements of each subset to the cost

Cost = 3 + 4 + 5 + 8 + 11 + 10 = 41

Greedy Approach

The idea of this approach is simple here we think greedy as we know that we have to minimize the cost by splitting the array into k subsets and adding ceil of k/2 highest elements to the cost so as per observation if sort the array then we are traversing the array from the backside. After that, for every subset of size k, we add k/2 highest elements to the cost we got to minimize cost.

Let's discuss this approach step by step below-

We have created a function calculateLowestCost that takes parameters such as the size of array, array, and k.

In the function, create a variable to "minimizeCost" to store the final answer.

Using the sort function sort the array

Store ceil of k/2 in the "updateK" variable

Travel for loop from n-1 to greater than equal to 0.

  • Store 'updateK' to the variable j to maintain the count of elements that we need to add in the cost ("minimizeCost")

  • store k to the variable tempK to maintain the subset of size k

  • Add j elements to the minimizeCost using a while loop and decrease the j, tempK, and i.

  • Update i for the subset of size k by subtracting the remaining tempK from i.

Return minimzeCost

Let's see the code below for a better understanding of the above approach.

Example

#include <bits/stdc++.h>
using namespace std;
 
// Create a function "calculateLowestCost"
int calculateLowestCost( int n, int array [], int k ) {
   // Create variable "minimizeCost" to store the final cost
   int minimizeCost = 0;
   // Using STL function sort, sorting an array
   sort(array , array + n);
   // Create variable updateK to store the ceil of k/2
   int updateK = ceil (k/2.0);
   // Traverse the array using for loop from the end as we need to add k/2 
   //highest element of each subset of size k
   for ( int i = n-1; i >= 0; i-- ) {
        // store ceil of k/2 to varible j
       int j = updateK;
       // store k to tempK to maintain the subset of size k
       int tempK = k;
      // Traverse the while loop to add ceil of k/2 highest element of the subset of size k
      while ( i>=0 && j--){
         minimizeCost += array[i];
         tempK --;
         i--;
      }
      // Update i for the subset of size k
      if(tempK){
         i -= tempK;
      }
      i++;
   }
   // Return Final cost
   return minimizeCost;
}
int main(){
   int n = 4; // Givn the siz of the array
   int array [] = { 3, 4, 2, 1 }; // Given an array
   int k = 2; // given an integer k
   // Create a variable 'res' to store minimize cost by calling the function "calculateMinimizeCost"
   int res = calculateLowestCost(n, array, k);
   cout<< "Minimize Cost for the Given array is " ;
   cout << res ;
   return 0;
}

Output

Minimize Cost for the Given array is 6

Time and Space Complexity

The time complexity of the above code is O(N * (logN)), as we traverse the array and use the sort function.

The space complexity of the above code is O(1), as no extra space is used for storing anything.

Where N is the size of the given array.

Conclusion

In this tutorial, we have implemented a C++ program to find the Minimize cost by splitting the given Array into subsets of size K and adding the highest K/2 elements of each subset into the cost. We have implemented a Greedy Approach and used a sorting function of STL. The time complexity is O(N * (log N), where N is the size of the given array and the space complexity of O(1) s no extra space is required.

Updated on: 31-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements