java || Simple implementation
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int[][] resultGrid(int[][] image, int threshold) {
        int m = image.length;
        int n = image[0].length;

        int[][] result = new int[m][n];
        int[][] count = new int[m][n];

        // Check all 3x3 subgrids
        for (int i = 0; i <= m - 3; i++) {
            for (int j = 0; j <= n - 3; j++) {
                boolean valid = true;

                // Check adjacent pairs inside the 3x3 block
                for (int r = i; r < i + 3 && valid; r++) {
                    for (int c = j; c < j + 3 && valid; c++) {
                        // Check right neighbor
                        if (c + 1 < j + 3 && Math.abs(image[r][c] - image[r][c + 1]) > threshold)
                            valid = false;
                        // Check down neighbor
                        if (r + 1 < i + 3 && Math.abs(image[r][c] - image[r + 1][c]) > threshold)
                            valid = false;
                    }
                }

                // If region is valid
                if (valid) {
                    int sum = 0;
                    for (int r = i; r < i + 3; r++) {
                        for (int c = j; c < j + 3; c++) {
                            sum += image[r][c];
                        }
                    }

                    int avg = sum / 9; // Round down

                    // Assign average to all pixels in region
                    for (int r = i; r < i + 3; r++) {
                        for (int c = j; c < j + 3; c++) {
                            result[r][c] += avg;
                            count[r][c]++;
                        }
                    }
                }
            }
        }

        // Final output generation
        int[][] output = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (count[i][j] == 0) {
                    output[i][j] = image[i][j]; // Unchanged
                } else {
                    output[i][j] = result[i][j] / count[i][j]; // Average of region averages
                }
            }
        }

        return output;
    }
}

```
['Java']