Easy & Clean Code in C++ | Java -- Have a look
Intuition\nC++ []\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int thres) {\n        int n = image.size(), m = ima
# Intuition\n```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int thres) {\n        int n = image.size(), m = image[0].size();\n        vector<vector<int>> sum(n, vector<int>(m, 0));\n        vector<vector<int>> cnt(n, vector<int>(m, 0));\n        for (int i = 0; i + 3 <= n; i++) {\n            for (int j = 0; j + 3 <= m; j++) {\n                bool is_good = true;\n                for (int qi = i; qi < i + 3; qi++)\n                    for (int pi = 0; pi < 2; pi++) {\n                        if (abs(image[qi][j + pi] - image[qi][j + pi + 1]) >\n                            thres)\n                            is_good = false;\n                    }\n                for (int qi = j; qi < j + 3; qi++)\n                    for (int pi = 0; pi < 2; pi++) {\n                        if (abs(image[i + pi][qi] - image[i + pi + 1][qi]) >\n                            thres)\n                            is_good = false;\n                    }\n                if (is_good) {\n                    int got = 0;\n                    for (int qi = i; qi < i + 3; qi++) {\n                        for (int qj = j; qj < j + 3; qj++) {\n                            got += image[qi][qj];\n                        }\n                    }\n                    got /= 9;\n                    for (int qi = i; qi < i + 3; qi++) {\n                        for (int qj = j; qj < j + 3; qj++) {\n                            sum[qi][qj] += got;\n                            cnt[qi][qj] += 1;\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<vector<int>> res(n, vector<int>(m, 0));\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                if (cnt[i][j] == 0)\n                    res[i][j] = image[i][j];\n                else\n                    res[i][j] = sum[i][j] / cnt[i][j];\n            }\n        }\n\n        return res;\n    }\n};\n```\n```java []\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int m = image.length, n = image[0].length;\n        int[][] result = new int[m][n];\n        int[][] count = new int[m][n];\n        for (int i = 0; i < m - 2; i++) {\n            for (int j = 0; j < n - 2; j++) {\n                int avg = isGrid(i, j, image, threshold);\n                if (avg > -1) {\n                    fillResult(i, j, result, count, avg);\n                }\n            }\n        }\n        makeAvg(result, count, image, m, n);\n        return result;\n    }\n\n    private void makeAvg(int[][] result, int count[][], int[][] image, int m, int n) {\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (count[i][j] == 0) {\n                    result[i][j] = image[i][j];\n                } else {\n                    result[i][j] = result[i][j] / count[i][j];\n                }\n            }\n        }\n    }\n\n    private void fillResult(int x, int y, int[][] result, int[][] count, int avg) {\n        for (int i = x; i < x + 3; i++) {\n            for (int j = y; j < y + 3; j++) {\n                result[i][j] += avg;\n                count[i][j]++;\n            }\n        }\n    }\n\n    private int isGrid(int x, int y, int image[][], int threshold) {\n        int avg = 0;\n        for (int i = x; i < x + 3; i++) {\n            for (int j = y; j < y + 3; j++) {\n                if (i < x + 2) {\n                    if (Math.abs(image[i][j] - image[i + 1][j]) > threshold) {\n                        return -1;\n                    }\n                }\n                if (j < y + 2) {\n                    if (Math.abs(image[i][j] - image[i][j + 1]) > threshold) {\n                        return -1;\n                    }\n                }\n                avg += image[i][j];\n            }\n        }\n        return avg / 9;\n    }\n}\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(N^2)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(n*m)\n# Code\n```\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int m = image.length, n = image[0].length;\n        int[][] result = new int[m][n];\n        int[][] count = new int[m][n];\n        for (int i = 0; i < m - 2; i++) {\n            for (int j = 0; j < n - 2; j++) {\n                int avg = isGrid(i, j, image, threshold);\n                if (avg > -1) {\n                    fillResult(i, j, result, count, avg);\n                }\n            }\n        }\n        makeAvg(result, count, image, m, n);\n        return result;\n    }\n\n    private void makeAvg(int[][] result, int count[][], int[][] image, int m, int n) {\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (count[i][j] == 0) {\n                    result[i][j] = image[i][j];\n                } else {\n                    result[i][j] = result[i][j] / count[i][j];\n                }\n            }\n        }\n    }\n\n    private void fillResult(int x, int y, int[][] result, int[][] count, int avg) {\n        for (int i = x; i < x + 3; i++) {\n            for (int j = y; j < y + 3; j++) {\n                result[i][j] += avg;\n                count[i][j]++;\n            }\n        }\n    }\n\n    private int isGrid(int x, int y, int image[][], int threshold) {\n        int avg = 0;\n        for (int i = x; i < x + 3; i++) {\n            for (int j = y; j < y + 3; j++) {\n                if (i < x + 2) {\n                    if (Math.abs(image[i][j] - image[i + 1][j]) > threshold) {\n                        return -1;\n                    }\n                }\n                if (j < y + 2) {\n                    if (Math.abs(image[i][j] - image[i][j + 1]) > threshold) {\n                        return -1;\n                    }\n                }\n                avg += image[i][j];\n            }\n        }\n        return avg / 9;\n    }\n}\n\n```\n# Please upvote if you find it helpful ... thanks or your like\n![th.jpeg](https://assets.leetcode.com/users/images/e1687e5e-c10d-40a1-bb7b-e89bedc9031b_1707026232.9843888.jpeg)\n\n
['C++', 'Java']