Separate Squares I - Problem

You are given a 2D integer array squares. Each squares[i] = [xi, yi, li] represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.

Find the minimum y-coordinate value of a horizontal line such that the total area of the squares above the line equals the total area of the squares below the line.

Note: Squares may overlap. Overlapping areas should be counted multiple times. Answers within 10^-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Case
$ Input: squares = [[1,1,3],[1,4,3],[3,1,1]]
Output: 2.5
💡 Note: Square 1: bottom-left (1,1), area 9. Square 2: bottom-left (1,4), area 9. Square 3: bottom-left (3,1), area 1. At y=2.5: above line = 9+9+1=19, below line = 0. This is just an example - actual calculation considers partial intersections.
Example 2 — Single Square
$ Input: squares = [[0,0,4]]
Output: 2.0
💡 Note: One square from (0,0) to (4,4) with area 16. The horizontal line at y=2 divides it exactly in half: 8 area above, 8 area below.
Example 3 — Non-overlapping
$ Input: squares = [[0,0,2],[4,0,2]]
Output: 1.0
💡 Note: Two separate squares, each with area 4. Line at y=1 divides each square in half, giving 4 area above and 4 area below.

Constraints

  • 1 ≤ squares.length ≤ 105
  • squares[i].length == 3
  • 1 ≤ xi, yi, li ≤ 109

Visualization

Tap to expand
Separate Squares: Find Balance LineSquare 1Square 2Square 3Balance Line y = ?Area AboveArea BelowGoal: Area Above = Area Below• Count overlapping areas multiple times• Handle partial square intersections• Find minimum valid y-coordinateOutput: Y-coordinate of the balance line
Understanding the Visualization
1
Input Squares
Each square defined by bottom-left point and side length
2
Find Balance Line
Horizontal line dividing equal areas above and below
3
Output Y-Coordinate
Return the y-coordinate of the balance line
Key Takeaway
🎯 Key Insight: Use binary search on y-coordinates to efficiently find where total areas above and below the line are equal
Asked in
Google 25 Amazon 18 Microsoft 12
23.4K Views
Medium Frequency
~35 min Avg. Time
890 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