C++ Just Implementation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nStoring averages for ea
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStoring averages for each cell and then taking average for each cell\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    \n    vector<int> dx = {0, +1, 0, -1};\n    vector<int> dy = {+1, 0, -1, 0};\n    \n    bool check(int i,  int j, int rangeX, int rangeY, vector<vector<int>>& image, int t){\n        for(int k=0; k<4; k++){\n            int newX = i+dx[k], newY = j+dy[k];\n            if(newX>=0 && newY>=0 && newX<image.size() && newY<image[0].size() && newX<=rangeX && newY<=rangeY && newX>=rangeX-2 && newY>=rangeY-2){\n                if(abs(image[i][j]-image[newX][newY])>t) return false;\n            }\n        }\n        \n        return true;\n    }\n    \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size(), m = image[0].size();\n        \n        vector<vector<vector<int>>> average(n, vector<vector<int>>(m, vector<int>()));\n        \n        for(int i=2; i<n; i++){\n            for(int j=2; j<m; j++){\n                \n                bool isRegion = true;\n                int sum = 0;\n                \n                for(int x=i-2; x<=i; x++){\n                    for(int y=j-2; y<=j; y++){\n                        sum += image[x][y];\n                        if(!check(x, y, i, j, image, threshold)){\n                            isRegion = false;\n                            break;\n                        }\n                    }\n                    if(!isRegion) break;\n                }\n                \n                if(isRegion){\n                    int avg = sum/9;\n                    \n                    for(int x=i-2; x<=i; x++){\n                        for(int y=j-2; y<=j; y++){\n                            average[x][y].push_back(avg);\n                        }\n                    }\n                }\n            }\n        }\n        \n        vector<vector<int>> ans(n, vector<int>(m, 0));\n        for(int i=0; i<n; i++){\n            for(int j=0; j<m; j++){\n                vector<int>& v = average[i][j];\n                if((int)v.size() == 0) ans[i][j] = image[i][j];\n                else{\n                    int sum = 0;\n                    int count = v.size();\n                    \n                    for(int val: v) sum += val;\n                    ans[i][j] = sum/count; \n                }\n            }\n        }\n        \n    \n        \n        return ans;\n    }\n};\n```
['C++']