Separate Squares I — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Separate Squares I
Detailed explanation and reference materials
Problem Overview

🟦 Separate Squares I

Difficulty: 🟡 Medium
Topics: Geometry, Binary Search, Prefix Area


📘 Problem Statement

You are given a 2D integer array squares.

Each square is represented as:

squares[i] = [xᵢ, yᵢ, lᵢ]

Where:

  • (xᵢ, yᵢ) is the bottom-left coordinate
  • lᵢ is the side length
  • All squares are parallel to the x-axis

🎯 Objective

Find the minimum y-coordinate of a horizontal line such that:

🟥 Total area of squares above the line

🟦 Total area of squares below the line

📌 Important Notes

  • Squares may overlap
  • Overlapping areas are counted multiple times
  • Answers within \(10^{-5}\) of the correct value are accepted

🧠 Key Insight

As you move the horizontal line upward:

  • Area below the line increases
  • Area above the line decreases

This creates a monotonic relationship, making the problem suitable for:

🔍 Binary Search on y-coordinate


🧪 Examples

✅ Example 1

Input

squares = [[0,0,1],[2,2,1]]

Output

1.00000

Explanation

Any horizontal line between y = 1 and y = 2 splits the total area evenly.
The minimum valid y is:

✔️ 1.00000


✅ Example 2

Input

squares = [[0,0,2],[1,1,1]]

Output

1.16667

Explanation

Area PortionRed SquareBlue SquareTotal
Below7/6 × 21/62.5
Above5/6 × 25/62.5

✔️ Areas above and below are equal at:

y = 7/6 ≈ 1.16667


📊 Constraints

ConstraintValue
Number of squares(1 ≤ n ≤ 5 × 10^4)
Coordinates(0 ≤ xᵢ, yᵢ ≤ 10^9)
Side length(1 ≤ lᵢ ≤ 10^9)
Total area≤ (10^{12})

🚀 Summary

  • ✔️ Count partial square areas above & below a line
  • ✔️ Overlaps are counted multiple times
  • ✔️ Use binary search on y
  • ✔️ Precision up to 1e-5

Happy Coding! 💻✨

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
arrays
binary-search
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/separate-squares-i

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.