Lengthy But Duable || With Explanation || Brute Force Approach
\nbool validate(int x,int y,vector<vector<int>>& image, int threshold){\n    for(int i=x;i<x+3;i++){\n        for(int j=y;j<y+3;j++){\n            int dx[4] = {
```\nbool validate(int x,int y,vector<vector<int>>& image, int threshold){\n    for(int i=x;i<x+3;i++){\n        for(int j=y;j<y+3;j++){\n            int dx[4] = {-1,1,0,0};\n            int dy[4] = {0,0,-1,1};\n            int currVal = image[i][j];\n            for(int k=0;k<4;k++){\n                int ni = i + dx[k];\n                int nj = j + dy[k];\n                if((ni>=x and ni<x+3) and (nj>=y and nj<y+3)){\n                    int newCurrVal = image[ni][nj];\n                    if(abs(newCurrVal - currVal)<=threshold){\n                        continue;\n                    }else{\n                        return false;\n                    }\n                }\n            }\n        }\n    }\n    return true;\n}\n\nvoid storeSumAndCount(int x,int y,vector<vector<int>>& image, vector<vector<int>>&avg,vector<vector<int>>& count){\n    int sum = 0;\n    for(int i=x;i<x+3;i++){\n        for(int j=y;j<y+3;j++){\n           sum+=image[i][j];\n        }\n    }\n    int average = sum/9;\n    for(int i=x;i<x+3;i++){\n        for(int j=y;j<y+3;j++){\n            avg[i][j]+=average;\n            count[i][j]+=1;\n        }\n    }\n}\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size();\n        int m = image[0].size();\n        vector<vector<int>>avg(n,vector<int>(m,0));\n        vector<vector<int>>count(n,vector<int>(m,0));\n        for(int i=0;i<=n-3;i++){\n            for(int j=0;j<=m-3;j++){\n                // firstly check if matrix build of 3*3 from i,j point is valid\n                bool b = validate(i,j,image,threshold);\n                if(b){\n                    storeSumAndCount(i,j,image,avg,count);    \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                if(count[i][j]==0){\n                    ans[i][j] = image[i][j];\n                }else{\n                    ans[i][j] = (avg[i][j]/count[i][j]);\n                }\n            }\n        }\n        \n        return ans;\n    \n    }\n};\n```\n\nTime Complexity -> O(n*m*(3X3)) -> which is eventually n*m\nSpace Complexity -> O(n*m) + O(n*m) = 2*n*m -> n*m
[]