Find the Grid of Region Average - Problem

You're given an m x n grayscale image represented as a 2D grid where each cell contains a pixel intensity value between 0 and 255. Your task is to identify smooth regions within the image and calculate regional averages.

A region is defined as any 3 x 3 subgrid where adjacent pixels (sharing an edge) have intensity differences ≤ threshold. Think of this as finding areas where the image transitions are smooth enough to be considered uniform.

Key Rules:

  • Each pixel can belong to multiple overlapping regions
  • For pixels in multiple regions: average the floor values of each region's average
  • For pixels in no regions: keep original intensity value
  • Always use floor division for final results

Return the processed image where each pixel shows its regional average intensity.

Input & Output

example_1.py — Basic Valid Region
$ Input: image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
Output: [[9,9,9,10],[9,9,9,10],[9,9,9,10]]
💡 Note: The 3x3 region in the top-left corner has all adjacent pixels differing by ≤ 3, so all pixels in this region get the average value floor(81/9) = 9. The rightmost column pixels don't belong to any valid region, so they keep their original values.
example_2.py — No Valid Regions
$ Input: image = [[10,20,30],[15,25,35],[20,30,40]], threshold = 3
Output: [[10,20,30],[15,25,35],[20,30,40]]
💡 Note: Adjacent pixels differ by more than threshold=3 (e.g., |10-20|=10 > 3), so no valid 3x3 regions exist. All pixels retain their original values.
example_3.py — Multiple Overlapping Regions
$ Input: image = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], threshold = 0
Output: [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
💡 Note: All pixels have the same value, so every 3x3 region is valid with average 1. Each pixel belongs to multiple regions, but all regions have the same average, so the result is unchanged.

Constraints

  • 3 ≤ m, n ≤ 300
  • 0 ≤ image[i][j] ≤ 255
  • 0 ≤ threshold ≤ 255
  • Adjacent pixels are those that share an edge (not diagonal)
  • All averages should be calculated using floor division

Visualization

Tap to expand
Find the Grid of Region Average INPUT Original Image (3x4 grid) 5 6 7 10 8 9 10 10 11 12 13 10 Valid 3x3 Region Parameters: image = [[5,6,7,10], [8,9,10,10], [11,12,13,10]] threshold = 3 Blue = smooth region, Red = excluded ALGORITHM STEPS 1 Find all 3x3 subgrids Check each possible 3x3 region 2 Validate smooth regions Adjacent diff <= threshold Region (0,0): |5-6|=1, |6-7|=1... All diffs <= 3 --> Valid! Avg = floor(81/9) = 9 3 Calculate region averages Sum pixels, divide by 9, floor Region at (0,0): (5+6+7+8+9+ 10+11+12+13)/9 = 81/9 = 9 4 Handle overlapping pixels Avg the averages for multi-region Pixel (1,1) in 2 valid regions Result = floor((9+9)/2) = 9 FINAL RESULT Processed Image 9 9 9 10 9 9 9 10 9 9 9 10 Legend: In valid region (avg=9) No valid region (original) Output: [[9,9,9,10], [9,9,9,10],[9,9,9,10]] OK Key Insight: A valid 3x3 region requires ALL adjacent pixel pairs to have intensity difference <= threshold. Column 3 (values 10,10,10) creates invalid transitions with column 2 (7,10,13) since |7-10|=3 but |13-10|=3 passes. However, the 3x3 at (0,1) fails because |7-10|>3 vertically. Result: only one valid region! TutorialsPoint - Find the Grid of Region Average | Optimal Solution
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
28.8K Views
Medium Frequency
~25 min Avg. Time
1.2K 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