Minimum sum absolute difference pairs
Detailed guide and Python implementation for the 'Minimum sum absolute difference pairs' problem.
Problem Statement
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.
- •1 <= len(arr1) == len(arr2) <= 10^5
- •-10^9 <= arr1[i], arr2[i] <= 10^9
Examples
min_sum_abs_diff_pairs([3, 2, 1], [2, 1, 3])
0
Sort both arrays: [1, 2, 3] and [1, 2, 3]. Diffs: |1-1| + |2-2| + |3-3| = 0.
min_sum_abs_diff_pairs([4, 1, 8, 7], [2, 3, 6, 5])
6
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?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.