Check if Grid can be Cut into Sections — 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 Check if Grid can be Cut into Sections
Detailed explanation and reference materials
Problem Overview

Check if Grid can be Cut into Sections

Problem Description

You are given an integer n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] = [startx, starty, endx, endy] represents a rectangle on the grid. Each rectangle is defined as follows:

  • (startx, starty): The bottom-left corner of the rectangle.
  • (endx, endy): The top-right corner of the rectangle.

Conditions to Satisfy:

Your task is to determine if it is possible to make either two horizontal or two vertical cuts on the grid such that:

  1. Each of the three resulting sections formed by the cuts contains at least one rectangle.
  2. Every rectangle belongs to exactly one section (i.e., no rectangle spans across a cut).

Return:

  • true if such cuts can be made.
  • false otherwise.

Examples & Explanation

Example 1

Input:

plaintext
n = 5
rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]

Grid Representation:

FDSSD

Explanation:

We can make horizontal cuts at y = 2 and y = 4, forming three sections:

  • Section 1: Below y = 2 (contains R1)
  • Section 2: Between y = 2 and y = 4 (contains R2 and R3)
  • Section 3: Above y = 4 (contains R4)

Since all conditions are satisfied, output is true.

Constraints

3n1093 \leq n \leq 10^9 3rectangles.length1053 \leq \text{rectangles.length} \leq 10^5 0rectangles[i][0]<rectangles[i][2]n0 \leq \text{rectangles}[i][0] < \text{rectangles}[i][2] \leq n 0rectangles[i][1]<rectangles[i][3]n0 \leq \text{rectangles}[i][1] < \text{rectangles}[i][3] \leq n

Rectangles do not overlap.

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

Categories
arrays
sorting
java
Reference Link
https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/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.