Maximum Points Inside the Square - Problem

You are given a 2D array points where points[i] represents the coordinates of point i, and a string s where s[i] represents the tag of point i.

A valid square is a square that:

  • Is centered at the origin (0, 0)
  • Has edges parallel to the axes
  • Does not contain two points with the same tag

Return the maximum number of points contained in a valid square.

Note: A point is considered to be inside the square if it lies on or within the square's boundaries. The side length of the square can be zero.

Input & Output

Example 1 — Basic Case
$ Input: points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca"
Output: 2
💡 Note: The square with maximum side length that doesn't contain duplicate tags has size 4 (distance 2 from origin), containing points [2,2] (tag 'a') and [-1,-2] (tag 'b'). Total: 2 points.
Example 2 — All Different Tags
$ Input: points = [[1,1],[-2,-2],[3,-1]], s = "abb"
Output: 1
💡 Note: The largest valid square can only contain 1 point because tags 'b' appear twice. The square of size 2 (distance 1) contains only [1,1] with tag 'a'.
Example 3 — Single Point
$ Input: points = [[1,1]], s = "a"
Output: 1
💡 Note: Only one point exists, so the maximum is 1.

Constraints

  • 1 ≤ points.length ≤ 105
  • points[i].length == 2
  • -109 ≤ points[i][0], points[i][1] ≤ 109
  • s.length == points.length
  • s consists of lowercase English letters

Visualization

Tap to expand
Maximum Points Inside Square ProblemInput PointsA(2,2)B(-1,-2)D(-4,4)C(-3,1)A(3,-3)Valid Square SearchABValid: 2 points (A, B)Invalid SquareABDCAInvalid: Duplicate AMaximum Valid Square: 2 pointsKey: Find largest square without duplicate tagsOptimal: O(n log n) using sorting or binary search
Understanding the Visualization
1
Input
Points with coordinates and tags
2
Process
Find largest square without duplicate tags
3
Output
Maximum number of points in valid square
Key Takeaway
🎯 Key Insight: The problem requires finding the maximum square size where each tag appears at most once
Asked in
Google 12 Amazon 8 Microsoft 6
3.4K Views
Medium Frequency
~25 min Avg. Time
89 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