 Open in App
Not now

# Minimum cost to make Array equal by increment/decrementing elements

• Difficulty Level : Easy
• Last Updated : 15 Nov, 2022

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++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;
int minCost(int arr[], int cost[], int n)
{
// Declare the maxSize variable to
// store max upper number
int maxSize = 1000002;

vector<int> total_cost_at_i(maxSize);

// Add the cost of all
// similar elements
for (int i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}

// Declare the prefix and suffix arrays
vector<int> prefix(maxSize);
vector<int> suffix(maxSize);

// Store the cost to convert all the
// elements before i to i
int sum = 0;
for (int i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the cost to convert all the
// elements after i + 1 to i
sum = 0;
for (int i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the minimum cost in the
// min_cost variable.
int min_cost = INT_MAX;
for (int i = 0; i < maxSize; i++) {
min_cost = min(min_cost, (prefix[i] + suffix[i]));
}
return min_cost;
}

// Driver code
int main()
{
int arr[] = { 1, 3, 5, 2 };
int cost[] = { 2, 3, 1, 14 };
int N = sizeof(arr) / sizeof(arr);

// Function Call
cout << minCost(arr, cost, N) << endl;

return 0;
}```

## Java

```// Java code to implement the approach
import java.io.*;
class GFG {

static int minCost(int[] arr, int[] cost, int n)
{

// Declare the maxSize variable to
// store max upper number
int maxSize = 1000002;

int[] total_cost_at_i = new int[maxSize];

// Add the cost of all
// similar elements
for (int i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}

// Declare the prefix and suffix arrays
int[] prefix = new int[maxSize];
int[] suffix = new int[maxSize];

// Store the cost to convert all the
// elements before i to i
int sum = 0;
for (int i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the cost to convert all the
// elements after i + 1 to i
sum = 0;
for (int i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the minimum cost in the
// min_cost variable.
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;

// Function call
System.out.println(minCost(arr, cost, N));
}
}

// This code is contributed by lokeshmvs21.```

## Python3

```# Python code to implement the approach
def minCost(arr, cost, n):

# Declare the maxSize variable to
# store max upper number
maxSize = 1000002

total_cost_at_i =  * maxSize

# Add the cost of all
# similar elements
for i in range(n):
total_cost_at_i[arr[i]] += cost[i]

# Declare the prefix and suffix arrays
prefix =  * maxSize
suffix =  * maxSize

# Store the cost to convert all the
# elements before i to i
sum = 0
for i in range(1, maxSize):
prefix[i] = prefix[i - 1] + sum
sum = sum + total_cost_at_i[i]

# Store the cost to convert all the
# elements after i + 1 to i
sum = 0
for i in range(maxSize - 2, -1, -1):
suffix[i] = suffix[i + 1] + sum
sum = sum + total_cost_at_i[i]

# Store the minimum cost in the
# min_cost variable.
min_cost = 1e9 + 7
for i in range(maxSize):
min_cost = min(min_cost, (prefix[i] + suffix[i]))

return min_cost

# Driver code
arr = [1, 3, 5, 2]
cost = [2, 3, 1, 14]
N = len(arr)

# Function Call
print(minCost(arr, cost, N))

# This code is contributed by Samim Hossain Mondal.
```

## C#

```// C# code to implement the approach
using System;
class GFG {

static int minCost(int[] arr, int[] cost, int n)
{

// Declare the maxSize variable to
// store max upper number
int maxSize = 1000002;

int[] total_cost_at_i = new int[maxSize];

// Add the cost of all
// similar elements
for (int i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}

// Declare the prefix and suffix arrays
int[] prefix = new int[maxSize];
int[] suffix = new int[maxSize];

// Store the cost to convert all the
// elements before i to i
int sum = 0;
for (int i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the cost to convert all the
// elements after i + 1 to i
sum = 0;
for (int i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the minimum cost in the
// min_cost variable.
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;

// Function call
Console.Write(minCost(arr, cost, N));
}
}

// This code is contributed by Pushpesh Raj.
```

## Javascript

```        // JavaScript code to implement the approach
const INT_MAX = 2147483647;

const minCost = (arr, cost, n) => {

// Declare the maxSize variable to
// store max upper number
let maxSize = 1000002;

let total_cost_at_i = new Array(maxSize).fill(0);

// Add the cost of all
// similar elements
for (let i = 0; i < n; i++) {
total_cost_at_i[arr[i]] += cost[i];
}

// Declare the prefix and suffix arrays
let prefix = new Array(maxSize).fill(0);
let suffix = new Array(maxSize).fill(0);

// Store the cost to convert all the
// elements before i to i
let sum = 0;
for (let i = 1; i < maxSize; i++) {
prefix[i] = prefix[i - 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the cost to convert all the
// elements after i + 1 to i
sum = 0;
for (let i = maxSize - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + sum;
sum = sum + total_cost_at_i[i];
}

// Store the minimum cost in the
// min_cost variable.
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;
}

// Driver code
let arr = [1, 3, 5, 2];
let cost = [2, 3, 1, 14];
let N = arr.length;

// Function Call
console.log(minCost(arr, cost, N));

// This code is contributed by rakeshsahni
```
Output

`8`

Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles