Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
Given an array arr[] having N integers from the range [1, N] and an integer K, the task is to find the minimum possible cost to split the array into non-empty subarrays that can be achieved based on the following conditions:
Examples:
Input: arr[] = {1, 2, 3, 1, 2, 3}, K = 2
Output: 4
Explanation:
Splitting the array into subarrays {1, 2, 3} and {1, 2, 3} generates the minimum cost, as none of the subarrays contain any repeating element.
All other splits will cost higher as one subarray will contain at least one repeating element.
Therefore, the minimum possible cost is 4.
Input: arr[] = {1, 2, 1, 1, 1}, K = 2
Output: 6
Naive Approach: The simplest idea to solve the problem is to generate all possible subarrays to precompute and store their respective costs. Then, calculate the cost for every possible split that can be performed on the array. Finally, print the minimum cost from all the splits.
Follow the steps below to solve the problem:
- Pre-compute the cost of every subarray based on the above conditions.
- Generate all possible split that can be performed on the array.
- For each split, calculate the total cost of every splitter subarray.
- Keep maintaining the minimum total cost generated and finally, print the minimum sum.
Time Complexity: O(N!)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use Dynamic Programming to optimize the above approach. Follow the steps below to solve the problem:
- Initialize an array dp[] of length N with INT_MAX at all indices.
- Initialize the first element of the array with zero.
- For any index i the array dp[i] represents the minimum cost to divide the array into subarrays from 0 to i.
- For each index i, count the minimum cost for all the indices from i to N.
- Repeat this process for all the elements of the array
- Return the last elements of dp[] to get the minimum cost of splitting the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int findMinCost(vector< int >& a, int k)
{
int n = ( int )a.size();
int max_ele = *max_element(a.begin(),
a.end());
ll dp[n + 1];
for ( int i = 1; i <= n; ++i)
dp[i] = INT_MAX;
dp[0] = 0;
for ( int i = 0; i < n; ++i) {
int freq[max_ele + 1];
memset (freq, 0, sizeof freq);
for ( int j = i; j < n; ++j) {
freq[a[j]]++;
int cost = 0;
for ( int x = 0;
x <= max_ele; ++x) {
cost += (freq[x] == 1)
? 0
: freq[x];
}
dp[j + 1] = min(dp[i] + cost + k,
dp[j + 1]);
}
}
return dp[n];
}
int main()
{
vector< int > arr = { 1, 2, 1, 1, 1 };
int K = 2;
cout << findMinCost(arr, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static long findMinCost( int [] a,
int k, int n)
{
int max_ele = Arrays.stream(a).max().getAsInt();
long [] dp = new long [n + 1 ];
for ( int i = 1 ; i <= n; ++i)
dp[i] = Integer.MAX_VALUE;
dp[ 0 ] = 0 ;
for ( int i = 0 ; i < n; ++i)
{
int [] freq = new int [max_ele + 1 ];
for ( int j = i; j < n; ++j)
{
freq[a[j]]++;
int cost = 0 ;
for ( int x = 0 ; x <= max_ele; ++x)
{
cost += (freq[x] == 1 ) ? 0 :
freq[x];
}
dp[j + 1 ] = Math.min(dp[i] + cost + k,
dp[j + 1 ]);
}
}
return dp[n];
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 1 , 1 , 1 };
int K = 2 ;
int n = arr.length;
System.out.print(findMinCost(arr,
K, n));
}
}
|
Python3
import sys
def findMinCost(a, k, n):
max_ele = max (a)
dp = [ 0 ] * (n + 1 )
for i in range ( 1 , n + 1 ):
dp[i] = sys.maxsize
dp[ 0 ] = 0
for i in range ( 0 , n):
freq = [ 0 ] * (max_ele + 1 )
for j in range (i, n):
freq[a[j]] + = 1
cost = 0
for x in range ( 0 , max_ele + 1 ):
cost + = ( 0 if (freq[x] = = 1 ) else
freq[x])
dp[j + 1 ] = min (dp[i] + cost + k,
dp[j + 1 ])
return dp[n]
if __name__ = = '__main__' :
arr = [ 1 , 2 , 1 , 1 , 1 ];
K = 2 ;
n = len (arr);
print (findMinCost(arr, K, n));
|
C#
using System;
using System.Linq;
class GFG{
static long findMinCost( int [] a,
int k, int n)
{
int max_ele = a.Max();
long [] dp = new long [n + 1];
for ( int i = 1; i <= n; ++i)
dp[i] = int .MaxValue;
dp[0] = 0;
for ( int i = 0; i < n; ++i)
{
int [] freq = new int [max_ele + 1];
for ( int j = i; j < n; ++j)
{
freq[a[j]]++;
int cost = 0;
for ( int x = 0; x <= max_ele; ++x)
{
cost += (freq[x] == 1) ? 0 :
freq[x];
}
dp[j + 1] = Math.Min(dp[i] + cost + k,
dp[j + 1]);
}
}
return dp[n];
}
public static void Main(String[] args)
{
int [] arr = {1, 2, 1, 1, 1};
int K = 2;
int n = arr.Length;
Console.Write(findMinCost(arr,
K, n));
}
}
|
Javascript
<script>
function findMinCost(a, k)
{
var n = a.length;
var max_ele = a.reduce((a, b) => Math.max(a, b))
var dp = Array(n + 1).fill(1000000000);
dp[0] = 0;
for ( var i = 0; i < n; ++i)
{
var freq = Array(max_ele + 1).fill(0);
for ( var j = i; j < n; ++j)
{
freq[a[j]]++;
var cost = 0;
for ( var x = 0; x <= max_ele; ++x)
{
cost += (freq[x] == 1) ? 0 : freq[x];
}
dp[j + 1] = Math.min(dp[i] + cost + k,
dp[j + 1]);
}
}
return dp[n];
}
var arr = [ 1, 2, 1, 1, 1 ];
var K = 2;
document.write(findMinCost(arr, K));
</script>
|
Time Complexity: O(N3), where N is the size of the given array.
Auxiliary Space: O(N)
Last Updated :
06 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...