Back to Practice Dashboard
Competitive ProgrammingEasy
Minimum sum absolute difference pairs
Learn how to solve the 'Minimum sum absolute difference pairs' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function min_sum_abs_diff_pairs(arr1, arr2) that takes two arrays of the same length, sorts them, and pairs corresponding elements to minimize the sum of their absolute differences. Return this minimum sum.
Constraints
- •1 <= len(arr1) == len(arr2) <= 10^5
- •-10^9 <= arr1[i], arr2[i] <= 10^9
Examples
Example 1
Input
min_sum_abs_diff_pairs([3, 2, 1], [2, 1, 3])
Output
0
Explanation
Sort both arrays: [1, 2, 3] and [1, 2, 3]. Diffs: |1-1| + |2-2| + |3-3| = 0.
Example 2
Input
min_sum_abs_diff_pairs([4, 1, 8, 7], [2, 3, 6, 5])
Output
6
Explanation
Sorted arrays: [1, 4, 7, 8] and [2, 3, 5, 6]. Diffs: |1-2| + |4-3| + |7-5| + |8-6| = 1 + 1 + 2 + 2 = 6.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.