Maximize the Distance Between Points on a Square
HARDDescription
You are given an integer side, representing the edge length of a square with corners at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
You are also given a positive integer k and a 2D integer array points, where points[i] = [xi, yi] represents the coordinate of a point lying on the boundary of the square.
You need to select k elements among points such that the minimum Manhattan distance between any two points is maximized.
Return the maximum possible minimum Manhattan distance between the selected k points.
The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
Example 1:
Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
Output: 2
Explanation:

Select all four points.
Example 2:
Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
Output: 1
Explanation:

Select the points (0, 0), (2, 0), (2, 2), and (2, 1).
Example 3:
Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
Output: 1
Explanation:

Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).
Constraints:
1 <= side <= 1094 <= points.length <= min(4 * side, 15 * 103)points[i] == [xi, yi]- The input is generated such that:
points[i]lies on the boundary of the square.- All
points[i]are unique.
4 <= k <= min(25, points.length)
Approaches
Checkout 3 different approaches to solve Maximize the Distance Between Points on a Square. Click on different approaches to view the approach and algorithm in detail.
Approach 1: Binary Search with Backtracking
The problem asks to maximize the minimum distance, which is a classic pattern for binary search on the answer. We can binary search for the minimum distance d. For a given d, we need to determine if it's possible to select k points such that the Manhattan distance between any two is at least d. This check, let's call it canPlace(d), can be solved using backtracking.
Algorithm
- Binary search for the answer
din the range[0, 2 * side]. - For each
d, callcanPlace(d)to check feasibility.canPlace(d)uses a backtracking helper functionsolve(count, start_index, selection).- Sort the input
pointsarray lexicographically. solve(count, start_index, selection):- If
count == k, a valid set is found, returntrue. - If
n - start_index < k - count, not enough points remain, returnfalse. - Iterate
ifromstart_indexton-1:- Check if
points[i]is at least distancedfrom all points inselection. - If it is, add
points[i]toselectionand callsolve(count + 1, i + 1, selection). - If the recursive call is successful, return
true. - Backtrack: remove
points[i]fromselection.
- Check if
- If the loop finishes, no solution found from this path, return
false.
- If
- If
canPlace(d)is true, we try for a largerd(low = mid + 1); otherwise, we need a smallerd(high = mid - 1).
The overall algorithm is to binary search for the answer d in the range [0, 2 * side]. The canPlace(d) function is the core of this approach.
To implement canPlace(d), we can define a recursive backtracking function that tries to build a valid set of k points. We first sort the points to have a consistent order, for example, lexicographically.
The backtracking function, say solve(k_needed, start_index, current_selection), would try to find k_needed more points starting from start_index in the sorted points array, given the current_selection.
- The base case for the recursion is when
k_neededis 0, which means we have successfully foundkpoints, so we returntrue. - If we run out of points to consider (
start_indexreaches the end) before findingkpoints, we returnfalse. - In the recursive step, we iterate from
start_indexto the end of the points array. For each pointp_i, we check if it's compatible with all points already incurrent_selection(i.e., its Manhattan distance to each is at leastd). - If
p_iis compatible, we add it to our selection and recurse to findk_needed - 1points from the rest of the array (solve(k_needed - 1, i + 1, ...)). - If the recursive call returns
true, we propagatetrueup. Otherwise, we backtrack by removingp_iand continue exploring other options.
This approach explores different combinations of k points, pruning branches that are invalid. However, its worst-case time complexity is exponential, making it too slow for the given constraints.
Complexity Analysis
Pros and Cons
- Conceptually simple and intuitive.
- A direct translation of the problem statement into a search algorithm.
- The time complexity is exponential in
kand polynomial inn, which is too slow for the given constraints (nup to 15000,kup to 25). - Likely to cause a Time Limit Exceeded (TLE) error.
Video Solution
Watch the video walkthrough for Maximize the Distance Between Points on a Square
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.