Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Minimum Cost to Make Arrays Identical

XPChallenge Points: 20
levelLevel: Medium

You are given two arrays arr and brr of the same length, and a number k.

You are allowed two types of operations on arr:

  1. Rearrangement Operation
    You can divide arr into multiple continuous pieces and reorder those pieces in any way.
    This entire rearrangement action costs k (no matter how many pieces you break into).

  2. Value Adjustment Operation
    You may increase or decrease any element in arr.
    If you change a value by x, the cost is x.

Your goal is to make arr exactly equal to brr while minimizing the total cost.

Return the minimum cost.

Example 1:

Input: arr = [1,2,3], brr = [1,2,3], k = 10

Output: 0

Explanation:

Arrays are already identical, so cost is 0.

Example 2:

Input: arr = [4, 10, -1], brr = [-1, 4, 10], k = 3

Output: 3

Explanation:

Rearrange arr → [-1, 4, 10], cost = 3. Now arr matches brr exactly, so no extra value adjustments needed. Total cost = 3.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result