Maximum Points Inside the Square - Problem
Maximum Points Inside the Square

Imagine you're a game designer creating a magical square arena centered at the origin (0, 0) with edges parallel to the coordinate axes. You have a collection of tagged points scattered across a 2D plane, where each point has unique coordinates and a character tag.

Your challenge: Find the largest possible square that contains the maximum number of points while following one crucial rule - no two points with the same tag can coexist within the square.

Input:
points: A 2D array where points[i] = [x, y] represents coordinates
s: A string where s[i] is the character tag for points[i]

Output: Return the maximum number of points that can fit in a valid square.

Key Rules:
• Square is centered at origin (0, 0)
• Square edges are parallel to axes
• Points on square boundaries count as inside
• Square side length can be zero (just the origin)
• No duplicate tags allowed within the square

Input & Output

example_1.py — Basic Case
$ Input: points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca"
Output: 2
💡 Note: The square with side length 4 (distance 2 from origin) contains points [2,2] with tag 'a' and [-1,-2] with tag 'b'. No duplicate tags, so we can include 2 points maximum.
example_2.py — All Points Valid
$ Input: points = [[1,1],[-2,-2],[3,-1]], s = "abb"
Output: 1
💡 Note: Points have tags 'a', 'b', 'b'. Since two points have tag 'b', we can only include one point with tag 'b'. The largest valid square contains just 1 point.
example_3.py — Origin Only
$ Input: points = [[1,1]], s = "a"
Output: 1
💡 Note: Single point case. The square of side length 2 (distance 1) contains the point [1,1] with tag 'a', giving us 1 point.

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
EntranceABCASecurity Zone Expansion✓ Current zone: 3 artworks (A, B, C)✗ Next expansion: conflict with duplicate Ad=70d=70d=28
Understanding the Visualization
1
Map the Gallery
Plot all artworks on coordinate grid with their sensor tags
2
Calculate Distances
Measure each artwork's distance from the entrance (origin)
3
Sort by Distance
Arrange artworks from closest to farthest from entrance
4
Expand Security Zone
Gradually increase square zone size, monitoring for sensor conflicts
5
Detect Conflict
Stop expansion when we encounter duplicate sensor codes
6
Return Maximum
Report the largest conflict-free zone size
Key Takeaway
🎯 Key Insight: The optimal square size is determined by sorting points by distance and finding the largest square that stops just before encountering any duplicate tag.
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
28.3K Views
Medium-High Frequency
~25 min Avg. Time
847 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