GFG App
Open App
Browser
Continue

# Minimum cost to make every Kth element in Array equal

Given an array arr[] of integers and an integer K, the task is to find the minimum number of operations required to make every Kth element in the array equal. While performing one operation you can either increase a number by one or decrease the number by one.

Examples:

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.

Approach: To solve the problem follow the below idea:

We need to find average of every Kth element and then find difference between the Kth element and average.

This could be implemented by following below mentioned steps:

• Initialize an array say average of size k for calculating the average of every Kth element.
• Iterate over the input array and then calculate the average of every kth element.
• Initialize a variable say, minCost. Iterate over the input array and add the absolute difference of arr[i] and average[i%k], minCost += abs(arr[i] – average[i % k]).

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std;` `int` `findCost(vector<``int``> arr, ``int` `k)` `{` `    ``int` `n = arr.size();` `    ``vector<``int``> average(k);` `    ``for` `(``int` `i = 0; i < k; i++) {` `        ``average[i] = arr[i];` `        ``int` `total_ele = 1;` `        ``for` `(``int` `j = i + k; j < n; j += k) {` `            ``average[i] = (average[i] + arr[j]);` `            ``total_ele++;` `        ``}`   `        ``// Calculating average` `        ``average[i] /= total_ele;` `    ``}` `    ``int` `minCost = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {`   `        ``// Adding the absolute difference` `        ``// to minCost variable` `        ``minCost += ``abs``(arr[i] - average[i % k]);` `    ``}` `    ``return` `minCost;` `}`   `// Drivers code` `int` `main()` `{`   `    ``// Input array` `    ``vector<``int``> arr = { 1, 2, 3, 4, 4, 6 };` `    ``int` `k = 3;`   `    ``// Function call` `    ``cout << findCost(arr, k);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the above approach:`   `import` `java.io.*;`   `class` `GFG {`   `    ``static` `int` `findCost(``int``[] arr, ``int` `k)` `    ``{` `        ``int` `n = arr.length;` `        ``int``[] average = ``new` `int``[k];` `        ``for` `(``int` `i = ``0``; i < k; i++) {` `            ``average[i] = arr[i];` `            ``int` `total_ele = ``1``;` `            ``for` `(``int` `j = i + k; j < n; j += k) {` `                ``average[i] += arr[j];` `                ``total_ele++;` `            ``}`   `            ``// Calculating average` `            ``average[i] /= total_ele;` `        ``}` `        ``int` `minCost = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++) {`   `            ``// Adding the absolute difference` `            ``// to minCost variable` `            ``minCost += Math.abs(arr[i] - average[i % k]);` `        ``}` `        ``return` `minCost;` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Input array` `        ``int``[] arr = { ``1``, ``2``, ``3``, ``4``, ``4``, ``6` `};` `        ``int` `k = ``3``;`   `        ``// Function call` `        ``System.out.print(findCost(arr, k));` `    ``}` `}`   `// This code is contributed by karthik.`

## Python3

 `# Python3 implementation of the above approach:`   `# Function to find the minimum number of operations ` `# required to make every Kth element in the array equal.` `def` `findCost(arr, k):` `    ``n ``=` `len``(arr)` `    ``average ``=` `[``0``] ``*` `k` `    ``for` `i ``in` `range``(k):` `        ``average[i] ``=` `arr[i]` `        ``total_ele ``=` `1` `        ``for` `j ``in` `range``(i ``+` `k, n, k):` `            ``average[i] ``+``=` `arr[j]` `            ``total_ele ``+``=` `1`   `        ``# Calculating average` `        ``average[i] ``/``/``=` `total_ele`   `    ``minCost ``=` `0` `    ``for` `i ``in` `range``(n):` `        ``# Adding the absolute difference` `        ``# to minCost variable` `        ``minCost ``+``=` `abs``(arr[i] ``-` `average[i ``%` `k])` `    ``return` `minCost`   `# Driver code` `arr ``=` `[``1``, ``2``, ``3``, ``4``, ``4``, ``6``]` `k ``=` `3`   `# Function call` `print``(findCost(arr, k))`

## C#

 `// C# implementation for the above approach:`   `using` `System;`   `public` `class` `GFG {`   `    ``static` `int` `findCost(``int``[] arr, ``int` `k)` `    ``{` `        ``int` `n = arr.Length;` `        ``int``[] average = ``new` `int``[k];` `        ``for` `(``int` `i = 0; i < k; i++) {` `            ``average[i] = arr[i];` `            ``int` `total_ele = 1;` `            ``for` `(``int` `j = i + k; j < n; j += k) {` `                ``average[i] += arr[j];` `                ``total_ele++;` `            ``}` `            ``// Calculating average` `            ``average[i] /= total_ele;` `        ``}` `        ``int` `minCost = 0;` `        ``for` `(``int` `i = 0; i < n; i++) {` `            ``// Adding the absolute difference to minCost` `            ``// variable` `            ``minCost += Math.Abs(arr[i] - average[i % k]);` `        ``}` `        ``return` `minCost;` `    ``}`   `    ``static` `public` `void` `Main()` `    ``{`   `        ``// Code` `        ``// Input array` `        ``int``[] arr = { 1, 2, 3, 4, 4, 6 };` `        ``int` `k = 3;`   `        ``// Function call` `        ``Console.Write(findCost(arr, k));` `    ``}` `}`   `// This code is contributed by lokesh.`

## Javascript

 `// JavaScript implementation of the above approach:`   `// Function to find the minimum number of operations ` `// required to make every Kth element in the array equal.` `function` `findCost(arr, k) {` `    ``let n = arr.length;` `    ``let average = ``new` `Array(k).fill(0);` `    ``for` `(let i = 0; i < k; i++) {` `        ``average[i] = arr[i];` `        ``let total_ele = 1;` `        ``for` `(let j = i + k; j < n; j += k) {` `            ``average[i] += arr[j];` `            ``total_ele += 1;` `        ``}`   `        ``// Calculating average` `        ``average[i] = Math.floor(average[i] / total_ele);` `    ``}` `    ``let minCost = 0;` `    ``for` `(let i = 0; i < n; i++) {` `        ``// Adding the absolute difference` `        ``// to minCost variable` `        ``minCost += Math.abs(arr[i] - average[i % k]);` `    ``}` `    ``return` `minCost;` `}`   `// Driver code` `let arr = [1, 2, 3, 4, 4, 6];` `let k = 3;`   `// Function call` `console.log(findCost(arr, k));`     `// This code is contributed by Tapesh(tapeshdua420)`

Output

`8`

Time Complexity: O(N)
Auxiliary Space: O(k) //For the array which was initialized for calculating the average.

My Personal Notes arrow_drop_up