Basic brute force
Intuition\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 
# Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nvector<int> xm{-1, +1, 0, 0};\nvector<int> ym{0, 0, -1, +1};\n\nbool check(int x1, int x2, int y1, int y2, int diff, vector<vector<int>>& g)\n{\n    int xn, yn;\n    \n    for(int i=x1; i<=x2; ++i)\n    {\n        for(int j=y1; j<=y2; ++j)\n        {\n            for(int k = 0; k<4; ++k)\n            {\n                xn = i + xm[k];\n                yn = j + ym[k];\n                \n                if(xn>=x1 && xn<=x2 && yn>=y1 && yn<=y2)\n                {\n                    int v1 = g[xn][yn];\n                    int v2 = g[i][j];\n                    \n                    if(abs(v1 - v2) > diff)\n                        return false;\n                }\n            }\n        } \n    }\n\n    return true;\n}\n\nint find(int x1, int x2, int y1, int y2, vector<vector<int>>& g)\n{\n    int res = 0;\n    \n    for(int i=x1; i<=x2; ++i)\n    {\n        for(int j=y1; j<=y2; ++j)\n        {\n            res+=g[i][j];\n        }\n    }    \n\n    res = res/9;\n    return res;\n}\n\n\n\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& g, int k) {\n        \n        int m = g.size(); int n = g[0].size();\n        map< pair<int, int>, vector<int>>mp;\n        \n        vector<int> em;\n        \n        for(int i=0; i<m; ++i)\n        {\n            for(int j=0; j<n; ++j)\n            {\n                mp[{i,j}] = em;\n            }\n        }\n        \n        \n        for(int x1=0, x2=2; x2<m; ++x1, ++x2)    \n        {\n            for(int y1=0, y2=2; y2<n; ++y1, ++y2)\n            {\n                if(check(x1, x2, y1, y2, k, g)) // check if condition is satisfied in that grid.\n                {\n                    int avg = find(x1, x2, y1, y2, g); // function to get average of the current 3x3 grid.\n                    \n                    for(int i=x1; i<=x2; ++i)\n                    {\n                        for(int j=y1; j<=y2; ++j)\n                        {\n                            mp[{i, j}].push_back(avg);\n                        }\n                    }\n                \n                }\n            \n            }\n        }\n        \n        vector<int> t1(n, 0);\n        vector<vector<int>> res(m, t1);\n        vector<int> curr;\n        \n        for(auto i = mp.begin(); i!=mp.end(); ++i)\n        {\n            int x = i->first.first;\n            int y = i->first.second;\n            \n            curr = i->second;\n            int sum = 0;\n            \n            for(int j=0; j<curr.size(); ++j)\n                sum+=curr[j];\n            \n            if(curr.size() == 0)\n                res[x][y] = g[x][y];\n            \n            else\n            {\n                sum = sum/curr.size();\n                res[x][y] = sum;\n            }                  \n        }\n        return res;    \n    }\n};\n```
['C++']