Beautiful Pairs - Problem
Beautiful Pairs is a fascinating geometric optimization problem that challenges you to find the pair of points with minimum Manhattan distance.
You are given two 0-indexed integer arrays
A pair of indices
๐ฏ Your Goal: Return the lexicographically smallest beautiful pair. If multiple pairs have the same minimum distance, return the one with the smallest
Example: If
You are given two 0-indexed integer arrays
nums1 and nums2 of the same length. Think of these as coordinates - each index i represents a point (nums1[i], nums2[i]) in 2D space.A pair of indices
(i, j) where i < j is called beautiful if the Manhattan distance between points i and j is the smallest among all possible pairs. The Manhattan distance is calculated as: |nums1[i] - nums1[j]| + |nums2[i] - nums2[j]|๐ฏ Your Goal: Return the lexicographically smallest beautiful pair. If multiple pairs have the same minimum distance, return the one with the smallest
i, and if there's still a tie, the smallest j.Example: If
nums1 = [1, 2, 3] and nums2 = [1, 2, 3], the points are (1,1), (2,2), (3,3). The distances are: (0,1)โ2, (0,2)โ4, (1,2)โ2. The minimum distance is 2, and (0,1) is lexicographically smaller than (1,2). Input & Output
example_1.py โ Basic Case
$
Input:
nums1 = [1, 2, 3], nums2 = [1, 2, 3]
โบ
Output:
[0, 1]
๐ก Note:
Points are (1,1), (2,2), (3,3). Distance between indices 0,1 is |1-2|+|1-2|=2. Distance between indices 0,2 is |1-3|+|1-3|=4. Distance between indices 1,2 is |2-3|+|2-3|=2. Both (0,1) and (1,2) have minimum distance 2, but (0,1) is lexicographically smaller.
example_2.py โ Same Points
$
Input:
nums1 = [1, 1, 2], nums2 = [1, 2, 1]
โบ
Output:
[0, 2]
๐ก Note:
Points are (1,1), (1,2), (2,1). Distance between indices 0,1 is |1-1|+|1-2|=1. Distance between indices 0,2 is |1-2|+|1-1|=1. Distance between indices 1,2 is |1-2|+|2-1|=2. Both (0,1) and (0,2) have minimum distance 1, but (0,1) is lexicographically smaller than (0,2), so we return (0,1). Wait, let me recalculate: (0,1) comes before (0,2) lexicographically, so the answer should be [0,1]. Actually, let me check: distance(0,1)=1, distance(0,2)=1, distance(1,2)=2. Between (0,1) and (0,2), we choose (0,1) as it's lexicographically smaller.
example_3.py โ All Different Distances
$
Input:
nums1 = [0, 5, 3], nums2 = [0, 0, 4]
โบ
Output:
[0, 2]
๐ก Note:
Points are (0,0), (5,0), (3,4). Distance between indices 0,1 is |0-5|+|0-0|=5. Distance between indices 0,2 is |0-3|+|0-4|=7. Distance between indices 1,2 is |5-3|+|0-4|=6. The minimum distance is 5, so we return [0,1].
Visualization
Tap to expand
Understanding the Visualization
1
Plot Points
Each index i represents a point (nums1[i], nums2[i]) on a 2D grid
2
Calculate Distances
For each pair, measure Manhattan distance: horizontal + vertical movement
3
Find Minimum
Identify pair(s) with the smallest total block distance
4
Break Ties
When multiple pairs have same distance, choose lexicographically smallest
Key Takeaway
๐ฏ Key Insight: Manhattan distance represents real-world "city block" distance. The optimal solution transforms coordinates to use geometric properties, achieving O(n log n) complexity instead of O(nยฒ) brute force.
Time & Space Complexity
Time Complexity
O(n log n)
Dominated by sorting step, closest pair finding is also O(n log n)
โก Linearithmic
Space Complexity
O(n)
Need to store transformed coordinates and maintain original indices
โก Linearithmic Space
Constraints
- 2 โค nums1.length == nums2.length โค 105
- -104 โค nums1[i], nums2[i] โค 104
- All points (nums1[i], nums2[i]) are treated as distinct even if coordinates are the same
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code