Minimum cost to make every Kth element in Array equal
Last Updated :
24 Apr, 2023
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 <bits/stdc++.h>
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++;
}
average[i] /= total_ele;
}
int minCost = 0;
for ( int i = 0; i < n; i++) {
minCost += abs (arr[i] - average[i % k]);
}
return minCost;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 4, 6 };
int k = 3;
cout << findCost(arr, k);
return 0;
}
|
Java
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++;
}
average[i] /= total_ele;
}
int minCost = 0 ;
for ( int i = 0 ; i < n; i++) {
minCost += Math.abs(arr[i] - average[i % k]);
}
return minCost;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 4 , 6 };
int k = 3 ;
System.out.print(findCost(arr, k));
}
}
|
Python3
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
average[i] / / = total_ele
minCost = 0
for i in range (n):
minCost + = abs (arr[i] - average[i % k])
return minCost
arr = [ 1 , 2 , 3 , 4 , 4 , 6 ]
k = 3
print (findCost(arr, k))
|
C#
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++;
}
average[i] /= total_ele;
}
int minCost = 0;
for ( int i = 0; i < n; i++) {
minCost += Math.Abs(arr[i] - average[i % k]);
}
return minCost;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 4, 6 };
int k = 3;
Console.Write(findCost(arr, k));
}
}
|
Javascript
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;
}
average[i] = Math.floor(average[i] / total_ele);
}
let minCost = 0;
for (let i = 0; i < n; i++) {
minCost += Math.abs(arr[i] - average[i % k]);
}
return minCost;
}
let arr = [1, 2, 3, 4, 4, 6];
let k = 3;
console.log(findCost(arr, k));
|
Time Complexity: O(N)
Auxiliary Space: O(k) //For the array which was initialized for calculating the average.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...