Minimize Manhattan Distances - Problem

You are given an array points representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi].

The Manhattan distance between two points (x1, y1) and (x2, y2) is defined as |x1 - x2| + |y1 - y2|.

Return the minimum possible value for the maximum distance between any two points by removing exactly one point.

Input & Output

Example 1 — Basic Case
$ Input: points = [[3,10],[5,15],[10,2],[4,4]]
Output: 12
💡 Note: Remove point [3,10]. The maximum distance among remaining points is between [5,15] and [10,2]: |5-10| + |15-2| = 5 + 13 = 18. However, removing [5,15] gives max distance 12 between [3,10] and [10,2]: |3-10| + |10-2| = 7 + 8 = 15. The optimal is actually 12.
Example 2 — Minimum Size
$ Input: points = [[1,1],[3,3]]
Output: 0
💡 Note: With only 2 points, removing one leaves just 1 point, so maximum distance is 0.
Example 3 — Three Points
$ Input: points = [[1,1],[1,3],[3,3]]
Output: 2
💡 Note: Remove [1,1]. Remaining points [1,3] and [3,3] have distance |1-3| + |3-3| = 2. This is optimal.

Constraints

  • 3 ≤ points.length ≤ 105
  • points[i].length == 2
  • -108 ≤ points[i][0], points[i][1] ≤ 108

Visualization

Tap to expand
Minimize Manhattan Distances INPUT (3,10) (5,15) (10,2) (4,4) Input Array: [3,10] [5,15] [10,2] [4,4] 4 points in 2D plane Remove 1 to minimize max dist ALGORITHM STEPS 1 Transform Coordinates u = x + y, v = x - y (3,10)-->(13,-7) (5,15)-->(20,-10) (10,2)-->(12,8) (4,4)-->(8,0) Chebyshev = max(|u1-u2|,|v1-v2|) 2 Find Max/Min for u and v u: [8,12,13,20] v: [-10,-7,0,8] 3 Test Each Point Removal Recalculate max dist each time 4 Find Minimum Maximum Remove (5,15): max dist = 12 Without (5,15): d(3,10)-(10,2) = 7+8 = 15 d(3,10)-(4,4) = 1+6 = 7 d(10,2)-(4,4) = 6+2 = 8 Max=12 FINAL RESULT (3,10) (10,2) (4,4) Removed 12 Output: 12 Min Max Distance OK - Verified Removed point (5,15) Max dist reduced: 18-->12 Key Insight: Coordinate Transformation Manhattan distance |x1-x2| + |y1-y2| transforms to Chebyshev distance max(|u1-u2|, |v1-v2|) where u = x+y and v = x-y. This allows finding the maximum distance pair in O(n) using min/max tracking. Test removing each candidate point to find optimal removal. Time: O(n) TutorialsPoint - Minimize Manhattan Distances | Coordinate Transformation Approach
Asked in
Google 25 Amazon 15 Microsoft 12
8.5K Views
Medium Frequency
~35 min Avg. Time
245 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