
Let Arr[] = {1, 1, 1, 4, 1, 2, 4}, K=2
The given array can be split into two sub-arrays {1, 1, 1, 4}, {1, 2, 4}. The total cost will be for the first sub-array - 2+ 3(frequency of 1 is 3 in the first sub-array) + for the second sub-array - 2, Hence total cost is 7.
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains two integers, ‘N’ and ‘K’, denoting the length of the array and cost of each subarray, respectively.
The second line of the test case contains an array ‘Arr’ of ‘N’ integers.
For each test case, print a single integer ‘ANS’ representing the minimum cost of splitting the array.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
1 <= T <= 10
1 <= N <= 500
1 <= K <= 500
1 <= Arr[i] <= 500
Time limit: 1 sec
The basic idea is to generate all the possible subarrays and find the cost of every possible split and then returning the minimum cost from all of them.
The steps are as follows:
The basic idea of this approach is that we will compute the minimum cost of creating a subarray from 1 to i where i <= N and store the value of that in a dp array.
The steps are as follows:
The basic idea of this approach is that we will compute the minimum cost of creating a subarray from 1 to i where i <= N but using a top-down dp such that we don’t have to calculate the frequency every time.
The steps are as follows: