PIXELBANKv9.1.0
Menu

Largest Connected Region

Problem Statement

After performing connected component analysis, we often want to find the largest object (region with most pixels).

Given a 2D binary grid, use Union-Find to identify all connected foreground regions and return the size of the largest region (count of 1s in that region).

Applications

  • Finding dominant objects in a scene
  • Noise filtering (removing small components)
  • Main subject detection

Constraints

  • 1≤rows,cols≤1001 \leq rows, cols \leq 100
  • Grid contains only 0s and 1s

Example:

Input:
grid = [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]]
Output:
4
Reasoning:

Two regions of size 4 each. Return 4.

🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.