Rafiq loves to play with dominoes. On his birthday his father gifted him ‘N’ piles of dominoes, where each pile contains a positive number of dominoes stacked above.
Rafiq loves to play with piles if they are of equal heights, so his father decides to make changes to the piles. His father wonders for each consecutive window of length ‘K’ what is the minimum cost to make them all of the equal height. Hence his father wants to calculate the cost for each window.
The cost for removing and adding one domino on any pile is one unit.
Determine what is the cost to make all elements equal in a window of size 'K', for all windows of size 'K' in an 'N'-sized array/list 'heights'.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’ i.e., length of the pile and ‘K’ size of the window.
The next line of each test case contains 'N' numbers a1, a2, a3... aN denoting the initial height of each pile.
Output Format:
For each test case, output the minimum cost for each window.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
1 <= T <= 10
1 <= N <= 10000
1 <= K <= N
1 <= height[i] <= 10^5
where 'T' is the number of test cases, 'N' is the number of piles, 'K' is the size of the window as discussed above and 'height[i]' represents the height of each pile.
Time limit: 1 sec
1
6 2
2 4 1 1 2 3
2 3 0 1 1
All the windows or subarray of size 2 are [2,4] , [4,1] , [1,1] , [1,2] , [2,3]. The cost of each subarray is 2,3,0,1,1.
1
6 3
3 1 1 1 4 4
2 0 3 3
All the windows or subarray of size 3 are [3,1,1] , [1,1,1] , [1,1,4] , [1,4,4] , [2,3]. The cost of each subarray is 2,0,3,3.
Try to notice that the optimal cost to make all elements equal is at the median of that window.
Let's reduce our question to a fixed array instead of every subarray of size ‘K’ and try to find an answer specific to that array/list ‘height’, so we could do the same thing for each window, now, as mentioned in the hint, we need to calculate the sum of absolute difference of every element from its median, so after getting the new array, next step would be to calculate the cost of that window, so first we will sort the array and select the ‘K’/2 th element as we know the size of the new array will be ‘K’, now we could calculate the cost easily by iterating over the window.
Reference: proof for optimal cost to make all elements equal would be at its median value.
The steps are as follows:
O(K*log(K) * (‘N’ - ‘K’ + 1)). where ‘N’ is the length of the ‘pile’ and ‘K’ is the size of the window.
As we know there is a total ‘N’ - ‘K’ + 1 window in a total of size ‘K’ and as we are doing this for each window or subarray our overall time complexity will be O(‘K * log(K)’ * (‘N’ - ‘K’ + 1)).
O(N) where ‘N’ is the length of the array/list ‘pile’ .
As we are only traversing the array and storing only constant variables, also please note that we are ignoring the space complexity for sort functions we are using. As space complexity of sort depends on the method of the sort we are using. Hence, the overall space complexity is O(N).