Magicsheet logo

Minimum Cost to Make Arrays Identical

Medium
12.5%
Updated 8/1/2025

Asked by 2 Companies

Minimum Cost to Make Arrays Identical

1. What is this problem about?

The Minimum Cost to Make Arrays Identical problem presents two arrays, arr and brr, of the same length. You are allowed to perform two types of operations. First, you can rearrange the elements of arr in any order you want for a fixed one-time cost KK. Second, you can increment or decrement any element of arr by 1 at a cost of 1 per unit change. The goal is to make arr identical to brr with the minimum total cost. This problem asks you to decide whether the flexibility of rearrangement is worth the cost KK.

2. Why is this asked in interviews?

This question is popular in Amazon and Google interviews because it tests a candidate's ability to evaluate different scenarios and use greedy logic to minimize costs. The Minimum Cost to Make Arrays Identical interview question requires comparing two distinct strategies: element-wise adjustment without reordering vs. optimal adjustment after reordering. It assesses whether you can identify that sorting both arrays is the best way to minimize the cost of making them identical if rearrangement is allowed.

3. Algorithmic pattern used

The problem uses a Greedy approach and Sorting. There are two primary cases to consider:

  1. No Rearrangement: Calculate the cost by summing the absolute differences arr[i]brr[i]|arr[i] - brr[i]| for all ii.
  2. With Rearrangement: Sort both arr and brr. After sorting, the cost to make them identical is the sum of arrsorted[i]brrsorted[i]|arr_{sorted}[i] - brr_{sorted}[i]| plus the fixed cost KK. The answer is the minimum of these two values. This "Array, Sorting, Greedy interview pattern" is a classic example of how sorting can simplify the process of matching elements between two sets.

4. Example explanation

Let arr = [10, 20], brr = [21, 11], and K=5K = 5.

  • Option 1 (No Rearrangement):
    • Cost to change 10 to 21: 11
    • Cost to change 20 to 11: 9
    • Total = 20.
  • Option 2 (Rearrange first):
    • Sort arr: [10, 20]
    • Sort brr: [11, 21]
    • Cost to change 10 to 11: 1
    • Cost to change 20 to 21: 1
    • Fixed cost: 5
    • Total = 1 + 1 + 5 = 7. The minimum cost is 7.

5. Common mistakes candidates make

A common error in the Minimum Cost to Make Arrays Identical coding problem is only considering the sorted case and forgetting that sometimes the fixed cost KK is so high that it's cheaper to just adjust the elements in their original positions. Another mistake is failing to realize that if you do rearrange, sorting both arrays is the optimal way to pair the elements. Some candidates might try complex matching algorithms or flow-based approaches, which are overkill and inefficient for a problem that can be solved with a simple greedy sort.

6. Interview preparation tip

Always look for "cost-benefit" trade-offs in interview questions. If an operation has a fixed cost but offers a structural advantage (like being able to reorder), calculate the best possible outcome with that advantage and compare it to the baseline. This "Greedy and Sorting interview pattern" is a recurring theme. Practice identifying when sorting minimizes the sum of absolute differences between two sequences; it's a powerful mathematical property often used in optimization problems.

Similar Questions