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 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].

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

Visualization

Tap to expand
Beautiful Pairs: Manhattan Distance VisualizationA(1,1)B(2,2)C(3,3)D(5,2)|1-2| + |1-2| = 2Distance CalculationsA-B: |1-2| + |1-2| = 2 โœ“A-C: |1-3| + |1-3| = 4A-D: |1-5| + |1-2| = 5B-C: |2-3| + |2-3| = 2 โœ“B-D: |2-5| + |2-2| = 3C-D: |3-5| + |3-2| = 3Minimum Distance: 2Pairs: (0,1) and (1,2)Lexicographically Smallest:[0, 1]๐ŸŽฏ Key Insight: Manhattan distance = sum of horizontal and vertical distances
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.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22 Apple 18
52.1K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen