Input: arr[] = {1, 2, 3, 4, 4, 6}, K = 3
Output: 8
Explanation: For the given array, value of K = 3 which means every 3rd element in the array should be equal. So we have to make 1 = 4, 2 = 4 and 3 = 6. So by performing 3 operations on 1 we can make it equal to 4 (or vice versa it will take same number of operations) and 2 operations for making 2 and 4 equal and 3 operations to make 6 and 3 equal .So total number of operations is 8 which is the minimum cost to make every 3rd element in array equal.
Input: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, K = 3
Output: 24
Explanation: Value of K = 3, so every 3rd element should be equal which means we have to make 1 = 4 = 7 = 10, 2 = 5 = 8 and 3 = 6 = 9. For making 1 = 4 = 7 = 10, the minimum cost will be 12 and for making 2 = 5 = 8, the minimum cost will be 6 and for making 3 = 6 = 9, the minimum cost will be 6. So for making every 3rd element of array equal we have to perform minimum 24 operations.