Maximize the Distance Between Points on a Square - Problem

Imagine you're a city planner tasked with placing k emergency stations on the perimeter of a square city block to maximize safety coverage. You want to ensure that the closest distance between any two stations is as large as possible.

Given a square with side length side and corners at (0, 0), (0, side), (side, 0), and (side, side), you have a list of candidate locations points on the boundary where stations can be placed. Each point is represented as [x, y].

Your goal is to select exactly k points from the available locations such that the minimum Manhattan distance between any two selected points is maximized.

The Manhattan distance between two points (x₁, y₁) and (x₂, y₂) is |x₁ - x₂| + |y₁ - y₂|.

Return the maximum possible minimum Manhattan distance between the selected k points.

Input & Output

example_1.py — Small Square
$ Input: side = 2, k = 2, points = [[0,0], [1,0], [2,0], [2,1], [2,2], [1,2], [0,2], [0,1]]
Output: 4
💡 Note: We can select points [0,0] and [2,2] (opposite corners). The Manhattan distance between them is |0-2| + |0-2| = 4, which is the maximum possible minimum distance for k=2 points.
example_2.py — More Points
$ Input: side = 3, k = 3, points = [[0,0], [0,1], [0,2], [0,3], [1,3], [2,3], [3,3], [3,2], [3,1], [3,0], [2,0], [1,0]]
Output: 4
💡 Note: We can select points [0,0], [3,1], and [1,3]. The minimum distance between any pair is 4, achieved by multiple pairs. This gives us optimal spacing for 3 points.
example_3.py — Edge Case
$ Input: side = 1, k = 4, points = [[0,0], [0,1], [1,1], [1,0]]
Output: 2
💡 Note: With only 4 corner points available and needing to select all 4, the minimum distance between adjacent corners is 2 (like [0,0] and [0,1]).

Constraints

  • 1 ≤ side ≤ 109
  • 2 ≤ k ≤ points.length ≤ 105
  • points[i] is on the boundary of the square
  • All points are distinct and lie exactly on the square's perimeter

Visualization

Tap to expand
Maximize Distance Between Points on a Square INPUT (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2) (0,1) Parameters side = 2 k = 2 (select 2 points) points = [ [0,0],[1,0],[2,0],[2,1], [2,2],[1,2],[0,2],[0,1] ] ALGORITHM (Greedy) 1 Binary Search Setup Search min dist: [0, 4*side] 2 Convert to Perimeter Map 2D points to 1D positions Perimeter positions: [0,1,2,3,4,5,6,7] (0 to 8) Total perimeter = 4*2 = 8 3 Greedy Selection For each mid, check if k points with min dist possible Try mid=4: Pick pos 0 Next valid: pos 4 (dist=4) OK: 2 points selected! 4 Binary Search Result Maximize valid min distance Answer: max feasible = 4 FINAL RESULT (0,0) (2,2) dist=4 Output 4 Manhattan distance: |0-2| + |0-2| = 4 Maximum possible min dist! Key Insight: The greedy approach works by mapping 2D boundary points to 1D perimeter positions, then using binary search on the answer. For each candidate distance, greedily select points with at least that spacing. On a square perimeter, opposite corners (0,0) and (2,2) give maximum Manhattan distance of 4 (half perimeter). TutorialsPoint - Maximize the Distance Between Points on a Square | Greedy Approach
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 18
20.2K Views
Medium Frequency
~25 min Avg. Time
856 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