Separate Squares II — 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 II
Detailed explanation and reference materials
Problem Overview

🟦 Separate Squares II

Difficulty: 🔴 Hard
Topics: Geometry, Sweep Line, Binary Search


🧩 Problem Statement

You are given a 2D integer array squares.

Each square is represented as:

squares[i] = [xᵢ, yᵢ, lᵢ]
  • (xᵢ, yᵢ) → bottom-left corner of the square
  • lᵢ → side length
  • All squares are axis-aligned (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


⚔️ Algorithm Face-Off: Separate Squares I vs II

FeatureSeparate Squares ISeparate Squares II
OverlapsCounted multiple timesCounted once
Area CalculationSimple sumUnion area
Geometry HandlingIndependent squaresInteracting squares
Main TechniqueBinary searchBinary search + sweep line
Data StructuresNoneTreeMap / interval merging
DifficultyMediumHard
Time Complexity(O(N log Y))(O(N log N log Y))

N = number of squares | Y = range of possible y-values

⚠️ Important Notes

  • Squares may overlap
  • Overlapping regions are counted only once
  • Answers within \(10^{-5}\) of the actual value are accepted

🖼️ Example 1

Input

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

Output

txt
1.00000

Explanation

Any horizontal line between y = 1 and y = 2 divides the total area equally:

  • ⬆️ Area above = 1
  • ⬇️ Area below = 1

✅ Minimum valid y-coordinate is 1


🖼️ Example 2

Input

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

Output

txt
1.00000

Explanation

The smaller square overlaps with the larger one.
Since overlapping areas are not double-counted, the horizontal line:

  • y = 1 splits the union area into two equal halves

📐 Constraints

  • 1 ≤ squares.length ≤ 5 × 10⁴
  • 0 ≤ xᵢ, yᵢ ≤ 10⁹
  • 1 ≤ lᵢ ≤ 10⁹
  • Total union area ≤ 10¹⁵

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

Categories
geometry
-sweep-line
binary-search
java
Reference Link
https://leetcode.com/problems/separate-squares-ii/description/

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.