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++;
}
// 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)
Time Complexity: O(N)
Auxiliary Space: O(k) //For the array which was initialized for calculating the average.
Similar Reads
Minimum cost to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to make all values of this array equal to some integer value with minimum cost after performing below operations any number of times (possibly zero). Reduce the array element by 2 or increase it by 2 with zero cost.Reduce the array
5 min read
Minimum Increment / decrement to make array elements equal Given an array of integers where 1 \leq A[i] \leq 10^{18} . In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal. Examples: Input : A[] = { 1, 5, 7, 10 } Output :
7 min read
Make all array elements equal with minimum cost Given an array of size n, the task is to make the value of all elements equal with minimum cost. The cost of changing a value from x to y is abs(x - y).Examples : Input: arr[] = [1, 100, 101]Output: 100Explanation: We can change all its values to 100 with minimum cost,|1 - 100| + |100 - 100| + |101
15 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Minimum cost to make all elements with same frequency equal Given an array arr[] of integers, the task is to find the minimum cost for making all the integers that have the same frequency equal. By performing one operation you can either increase the current integer by 1 or decrease it by 1. Examples: Input: arr[] = {1, 2, 3, 2, 6, 5, 6}Output: 12Explanation
9 min read