Problem Description
Given two 0-indexed integer arrays nums1 and nums2 of the same length, think of each index i as defining a point (nums1[i], nums2[i]). A pair of indices (i, j) with i < j is called beautiful if the Manhattan distance |nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is as small as possible over all index pairs. In case of multiple pairs with the same minimum distance, return the lexicographically smallest pair (i.e. the one with the smallest i, and if tied then smallest j).
Key Insights
- The distance metric is the Manhattan distance between points defined by corresponding elements in nums1 and nums2.
- The problem becomes a “closest pair” in the L1 (Manhattan) metric, which has similarities to the well–known Euclidean closest pair problem.
- A brute-force O(n²) solution is too slow for n up to 10⁵; hence an O(n log n) approach is needed.
- A sweep-line (or divide and conquer) algorithm can be used. First, sort the points by one coordinate (e.g. by nums1 value), then maintain an “active set” ordered by the other coordinate (nums2) containing candidates within the current best distance.
- When processing a new point, remove points from the active set whose x-coordinate difference exceeds the current best distance. Then, search only among points whose y-coordinate is within a band determined by the current best distance.
- Don’t forget to update the answer with pair indices (in sorted order so that i < j) and, in case of ties, choose the lexicographically smallest one.
Space and Time Complexity
Time Complexity: O(n log n) on average, due to sorting the points and performing logarithmic insertions and removals from the balanced tree (or ordered set) for the sweep‐line. Space Complexity: O(n) for storing the points and the active set.
Solution
We can solve the problem by first representing each point as (x, y, index) where x = nums1[i] and y = nums2[i]. We then sort the list of points by x. As we sweep from left to right (in increasing x), we maintain an active set (ordered by y) that contains only the points that are “close enough” in x to possibly yield a Manhattan distance smaller than our current best distance. For each new point, we remove outdated points from the active set, then use binary search to quickly iterate only over those candidates whose y difference is no more than the current best distance. For each candidate in the active set with index value, we compute the Manhattan distance and update the best distance and best pair (making sure to output the pair in lexicographical order). We then insert the current point into the active set before proceeding.
The same logic is implemented in all four languages below using: • An array (sorted by x) for the points. • An “active set” ordered by y (achieved via binary search in Python and JavaScript, and via built‑in ordered data structures in C++ and Java). • Careful updating of both the best (minimum) Manhattan distance and the lexicographically smallest index pair in case of ties.