Number of Black Blocks - Problem
Grid Color Block Counter

Imagine you're analyzing a digital art canvas represented as an m × n grid where certain cells are painted black while others remain white. Your task is to count how many 2×2 blocks contain exactly 0, 1, 2, 3, or 4 black cells.

Given:
• Grid dimensions: m rows and n columns
• Array of coordinates where each [x, y] represents a black cell
• All other cells are white by default

Goal: Return an array of size 5 where result[i] represents the count of 2×2 blocks containing exactly i black cells.

Example: In a 3×3 grid with black cells at [[0,0], [1,1]], you need to examine all possible 2×2 blocks and count how many contain 0, 1, 2, 3, or 4 black cells respectively.

Input & Output

example_1.py — Basic Grid
$ Input: m = 3, n = 3, coordinates = [[0,0], [1,1]]
Output: [3, 1, 1, 0, 0]
💡 Note: In a 3×3 grid, there are 4 possible 2×2 blocks. Block (0,0) contains 1 black cell at [0,0]. Block (0,1) contains 1 black cell at [1,1]. Block (1,0) contains 1 black cell at [1,1]. Block (1,1) contains 0 black cells. So we have 3 blocks with 0 black cells, 1 block with 1 black cell, 1 block with 1 black cell.
example_2.py — Dense Pattern
$ Input: m = 3, n = 3, coordinates = [[0,0], [1,1], [0,1], [1,0]]
Output: [0, 0, 0, 1, 0]
💡 Note: All 4 black cells form a 2×2 square. The only 2×2 block starting at (0,0) contains all 4 black cells, while blocks (0,1), (1,0), and (1,1) each contain some subset. Actually, let me recalculate: Block (0,0) has 4 blacks, so result[4] = 1, and the other 3 blocks have fewer.
example_3.py — Minimal Case
$ Input: m = 2, n = 2, coordinates = [[0,1]]
Output: [0, 1, 0, 0, 0]
💡 Note: Only one 2×2 block is possible in a 2×2 grid. It contains exactly 1 black cell at position [0,1], so result[1] = 1.

Visualization

Tap to expand
Result Breakdown:Blocks with 0 black cells: 3Blocks with 1 black cell: 1Blocks with 2 black cells: 0Blocks with 3 black cells: 0Blocks with 4 black cells: 0Output: [3, 1, 0, 0, 0]
Understanding the Visualization
1
Place Black Cells
Mark black cells on the grid
2
Identify Block Contributions
Each black cell affects up to 4 different 2×2 blocks
3
Count Block Types
Categorize blocks by their black cell counts
4
Generate Final Result
Return array with counts for 0-4 black cells per block
Key Takeaway
🎯 Key Insight: Instead of examining every block, track how black cells contribute to blocks - each black cell affects at most 4 blocks, making the solution scale with input size rather than grid size.

Time & Space Complexity

Time Complexity
⏱️
O(k)

Where k is the number of black cells. Each black cell contributes to at most 4 blocks

n
2n
Linear Growth
Space Complexity
O(min(k, m×n))

Hash map stores at most min(4k, (m-1)×(n-1)) block contributions

n
2n
Linearithmic Space

Constraints

  • 2 ≤ m, n ≤ 105
  • 0 ≤ coordinates.length ≤ 104
  • coordinates[i].length == 2
  • 0 ≤ coordinates[i][0] < m
  • 0 ≤ coordinates[i][1] < n
  • All coordinates in coordinates are distinct
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
23.4K Views
Medium Frequency
~18 min Avg. Time
892 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