Minimum cost to make Array equal by increment/decrementing elements
Given an array, arr[], and the cost array cost[], the task is to find the minimum cost to make all the array elements equal by incrementing or decrementing the element at any index by 1. The cost of the increment or decrement operation for the element at index i is the cost[i].
Examples:
Input: arr[] = {1, 3, 5, 2}, cost[] = {2, 3, 1, 14}
Output: 8
Explanation: On making all elements equal to 2 the cost is 1*2 + 1*3 + 3*1 = 8 which is the minimum cost.
Input: arr[] = {3, 3, 3, 3, 3}, cost[] = {1, 2, 3, 4, 5}
Output: 0
Explanation: All are same already in the array arr[].
Approach: This can be solved using the following idea.
The idea is to try converting each array to each of all values between [1, 106] and convert all to that number that gives minimum cost. We use prefix sum and suffix sum technique to store the cost to convert all elements to that index i and finally take the minimum of sum of prefix sum and suffix sum at index i.
Follow the steps mentioned below to solve the problem:
- Initialize a variable maxSize = 1000002 which stores the max size.
- Initialize an array total_cost_at_i[] of size maxSize which stores the total cost for similar arr[i].
- Now traverse through the array and add the costs of all similar elements in the array total_cost_at_i[].
- Take the variable sum=0 to store the sum of cost till i.
- Declare the two arrays prefix for storing the cost to convert all the previous i-1 elements to i and suffix to store the cost of converting all the elements from i+1 to maxSize to i.
- Now take the minimum of prefix[i] + suffix[i] at every index and store the minimum in the min_cost variable.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( int arr[], int cost[], int n)
{
int maxSize = 1000002;
vector< int > total_cost_at_i(maxSize);
for ( int i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}
vector< int > prefix(maxSize);
vector< int > suffix(maxSize);
int sum = 0;
for ( int i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}
sum = 0;
for ( int i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}
int min_cost = INT_MAX;
for ( int i = 0; i < maxSize; i++) {
min_cost = min(min_cost, (prefix[i] + suffix[i]));
}
return min_cost;
}
int main()
{
int arr[] = { 1, 3, 5, 2 };
int cost[] = { 2, 3, 1, 14 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minCost(arr, cost, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minCost( int [] arr, int [] cost, int n)
{
int maxSize = 1000002 ;
int [] total_cost_at_i = new int [maxSize];
for ( int i = 0 ; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}
int [] prefix = new int [maxSize];
int [] suffix = new int [maxSize];
int sum = 0 ;
for ( int i = 1 ; i < maxSize; i++) {
prefix[i] = prefix[i - 1 ] + sum;
sum = sum + total_cost_at_i[i];
}
sum = 0 ;
for ( int i = maxSize - 2 ; i >= 0 ; i--) {
suffix[i] = suffix[i + 1 ] + sum;
sum = sum + total_cost_at_i[i];
}
int min_cost = Integer.MAX_VALUE;
for ( int i = 0 ; i < maxSize; i++) {
min_cost = Math.min(min_cost,
(prefix[i] + suffix[i]));
}
return min_cost;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 2 };
int [] cost = { 2 , 3 , 1 , 14 };
int N = arr.length;
System.out.println(minCost(arr, cost, N));
}
}
|
Python3
def minCost(arr, cost, n):
maxSize = 1000002
total_cost_at_i = [ 0 ] * maxSize
for i in range (n):
total_cost_at_i[arr[i]] + = cost[i]
prefix = [ 0 ] * maxSize
suffix = [ 0 ] * maxSize
sum = 0
for i in range ( 1 , maxSize):
prefix[i] = prefix[i - 1 ] + sum
sum = sum + total_cost_at_i[i]
sum = 0
for i in range (maxSize - 2 , - 1 , - 1 ):
suffix[i] = suffix[i + 1 ] + sum
sum = sum + total_cost_at_i[i]
min_cost = 1e9 + 7
for i in range (maxSize):
min_cost = min (min_cost, (prefix[i] + suffix[i]))
return min_cost
arr = [ 1 , 3 , 5 , 2 ]
cost = [ 2 , 3 , 1 , 14 ]
N = len (arr)
print (minCost(arr, cost, N))
|
C#
using System;
class GFG {
static int minCost( int [] arr, int [] cost, int n)
{
int maxSize = 1000002;
int [] total_cost_at_i = new int [maxSize];
for ( int i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}
int [] prefix = new int [maxSize];
int [] suffix = new int [maxSize];
int sum = 0;
for ( int i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}
sum = 0;
for ( int i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}
int min_cost = Int32.MaxValue;
for ( int i = 0; i < maxSize; i++) {
min_cost = Math.Min(min_cost,
(prefix[i] + suffix[i]));
}
return min_cost;
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 5, 2 };
int [] cost = { 2, 3, 1, 14 };
int N = arr.Length;
Console.Write(minCost(arr, cost, N));
}
}
|
Javascript
const INT_MAX = 2147483647;
const minCost = (arr, cost, n) => {
let maxSize = 1000002;
let total_cost_at_i = new Array(maxSize).fill(0);
for (let i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}
let prefix = new Array(maxSize).fill(0);
let suffix = new Array(maxSize).fill(0);
let sum = 0;
for (let i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}
sum = 0;
for (let i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}
let min_cost = INT_MAX;
for (let i = 0; i < maxSize; i++) {
min_cost = Math.min(min_cost, (prefix[i] + suffix[i]));
}
return min_cost;
}
let arr = [1, 3, 5, 2];
let cost = [2, 3, 1, 14];
let N = arr.length;
console.log(minCost(arr, cost, N));
|
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(N)
Last Updated :
15 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...