Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Greedy
Minimum Cost To Make Arrays Identical

Make two arrays identical by changing values with minimum total cost.

Acceptance 0%
Problem Statement

Problem

You are given two integer arrays of the same length. In one operation, you may change an element in either array to any other integer value, and the cost of that change is the absolute difference between the old value and the new value.

Your task is to compute the minimum total cost needed so that the two arrays become identical element-by-element after all changes.

In many formulations of this problem, the two arrays are first sorted, and the best matching between elements is used to minimize the total cost. Return the minimum achievable cost.

Notes

  • The arrays must end up with the same multiset of values if reordering is allowed, or the same values at each position if the problem statement fixes positions.
  • For this record, treat the task as the common interview variant where the goal is to minimize the sum of absolute differences under optimal pairing/matching.

Input Format

  • An integer n, the length of both arrays.
  • An array nums1 of length n.
  • An array nums2 of length n.

If the original platform statement includes additional constraints, they are not available here; use the standard equal-length integer array interpretation.

Output Format

  • Return an integer representing the minimum total cost to make the arrays identical.

Constraints

  • Both arrays have the same length.
  • Elements are integers.
  • The cost of changing a value x to y is |x - y|.
  • The intended solution should avoid brute-force matching over all permutations.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.