Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset
Last Updated :
30 Sep, 2022
Given an array arr[] of size N and a positive integer K, the task is to find the minimum possible cost to split the array into K subsets, where the cost of ith element ( 1-based indexing ) of each subset is equal to the product of that element and i.
Examples:
Input: arr[] = { 2, 3, 4, 1 }, K = 3
Output: 11
Explanation:
Split the array arr[] into K(= 3) subsets { { 4, 1 }, { 2 }, { 3 } }
Total cost of 1st subset = 4 * 1 + 1 * 2 = 6
Total cost of 2nd subset = 2 * 1 = 2
Total cost of 3rd subset = 3 * 1 = 3
Therefore, the total cost of K(= 3) subsets is 6 + 2 + 3 = 11.
Input: arr[] = { 9, 20, 7, 8 }, K=2
Output: 59
Explanation:
Dividing the array arr[] into K(= 3) subsets { { 20, 8 }, { 9, 7 } }
Total cost of 1st subset = 20 * 1 + 8 * 2 = 36
Total cost of 2nd subset = 9 * 1 + 7 * 2 = 23
Therefore, the total cost of K(= 3) subsets is 36 + 23 = 59
Approach: The problem can be solved using Greedy technique. The idea is to divide the array elements such all elements in respective subsets is in decreasing order. Follow the steps below to solve the problem:
- Sort the given array in descending order.
- Initialize a variable, say totalCost, to store the minimum cost to split the array into K subsets.
- Initialize a variable, say X, to store the position of an element in a subset.
- Iterate over the range [1, N] using variable i. For every ith operation, increment the value of totalCost by ((arr[i]+ ...+ arr[i + K]) * X) and update i = i + K, X += 1.
- Finally, print the value of totalCost.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum cost to
// split array into K subsets
int getMinCost(int* arr, int n, int k)
{
// Sort the array in descending order
sort(arr, arr + n, greater<int>());
// Stores minimum cost to split
// the array into K subsets
int min_cost = 0;
// Stores position of
// elements of a subset
int X = 0;
// Iterate over the range [1, N]
for (int i = 0; i < n; i += k) {
// Calculate the cost to select
// X-th element of every subset
for (int j = i; j < i + k && j < n; j++) {
// Update min_cost
min_cost += arr[j] * (X + 1);
}
// Update X
X++;
}
return min_cost;
}
// Driver Code
int main()
{
int arr[] = { 9, 20, 7, 8 };
int K = 2;
int N = sizeof(arr)
/ sizeof(arr[0]);
// Function call
cout << getMinCost(arr, N, K) << endl;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// reverses an array
static void reverse(int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to find the minimum cost to
// split array into K subsets
static int getMinCost(int[] arr, int n, int k)
{
// Sort the array in descending order
Arrays.sort(arr);
reverse(arr, n);
// Stores minimum cost to split
// the array into K subsets
int min_cost = 0;
// Stores position of
// elements of a subset
int X = 0;
// Iterate over the range [1, N]
for (int i = 0; i < n; i += k)
{
// Calculate the cost to select
// X-th element of every subset
for (int j = i; j < i + k && j < n; j++)
{
// Update min_cost
min_cost += arr[j] * (X + 1);
}
// Update X
X++;
}
return min_cost;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 9, 20, 7, 8 };
int K = 2;
int N = arr.length;
// Function call
System.out.println( getMinCost(arr, N, K));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python program to implement
# the above approach
# Function to find the minimum cost to
# split array into K subsets
def getMinCost(arr, n, k):
# Sort the array in descending order
arr.sort(reverse = True)
# Stores minimum cost to split
# the array into K subsets
min_cost = 0;
# Stores position of
# elements of a subset
X = 0;
# Iterate over the range [1, N]
for i in range(0, n, k):
# Calculate the cost to select
# X-th element of every subset
for j in range(i, n, 1):
# Update min_cost
if(j < i + k):
min_cost += arr[j] * (X + 1);
# Update X
X += 1;
return min_cost;
# Driver code
if __name__ == '__main__':
arr = [9, 20, 7, 8];
K = 2;
N = len(arr);
# Function call
print(getMinCost(arr, N, K));
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// reverses an array
static void reverse(int []a, int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to find the minimum cost to
// split array into K subsets
static int getMinCost(int[] arr, int n, int k)
{
// Sort the array in descending order
Array.Sort(arr);
reverse(arr, n);
// Stores minimum cost to split
// the array into K subsets
int min_cost = 0;
// Stores position of
// elements of a subset
int X = 0;
// Iterate over the range [1, N]
for (int i = 0; i < n; i += k)
{
// Calculate the cost to select
// X-th element of every subset
for (int j = i; j < i + k && j < n; j++)
{
// Update min_cost
min_cost += arr[j] * (X + 1);
}
// Update X
X++;
}
return min_cost;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 9, 20, 7, 8 };
int K = 2;
int N = arr.Length;
// Function call
Console.WriteLine( getMinCost(arr, N, K));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Reverses an array
function reverse(a, n)
{
var i, k, t;
for(i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to find the minimum cost to
// split array into K subsets
function getMinCost(arr, n, k)
{
// Sort the array in descending order
arr.sort((a, b) => b - a);
// Stores minimum cost to split
// the array into K subsets
var min_cost = 0;
// Stores position of
// elements of a subset
var X = 0;
// Iterate over the range [1, N]
for(var i = 0; i < n; i += k)
{
// Calculate the cost to select
// X-th element of every subset
for(var j = i; j < i + k && j < n; j++)
{
// Update min_cost
min_cost += arr[j] * (X + 1);
}
// Update X
X++;
}
return min_cost;
}
// Driver code
var arr = [ 9, 20, 7, 8 ];
var K = 2;
var N = arr.length;
// Function call
document.write(getMinCost(arr, N, K));
// This code is contributed by rdtank
</script>
Time Complexity: O(N * log(N))
Auxiliary Space: O(1) it is using constant space for variables
Similar Reads
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2,
8 min read
Minimize cost by splitting given Array into subsets of size K and adding highest K/2 elements of each subset into cost Given an array arr[] of N integers and an integer K, the task is to calculate the minimum cost by spilling the array elements into subsets of size K and adding the maximum âK/2â elements into the cost. Note: âK/2â means ceiling value of K/2. Examples: Input: arr[] = {1, 1, 2, 2}, K = 2Output: 3Expla
5 min read
Split array into K-length subsets to minimize sum of second smallest element of each subset Given an array arr[] of size N and an integer K (N % K = 0), the task is to split array into subarrays of size K such that the sum of 2nd smallest elements of each subarray is the minimum possible. Examples: Input: arr[] = {11, 20, 5, 7, 8, 14, 2, 17, 16, 10}, K = 5Output: 13Explanation: Splitting a
5 min read
Minimize splits in given Array to find subsets of at most 2 elements with sum at most K Given an array arr[] of N integers and an integer K, the task is to calculate the minimum number of subsets of almost 2 elements the array can be divided such that the sum of elements in each subset is almost K. Examples: Input: arr[] = {1, 2, 3}, K = 3Output: 2Explanation: The given array can be di
6 min read
Maximize count of subsets into which the given array can be split such that it satisfies the given condition Given an array arr[] of size N and a positive integer X, the task is to partition the array into the maximum number of subsets such that the multiplication of the smallest element of each subset with the count of elements in the subsets is greater than or equal to K. Print the maximum count of such
12 min read