Easy to understand Soln
\n\n# Approach\neach region can be uniquely identified by its top left element.\n1. we will iterate over all the possible top left element of region.\n2. check 
\n\n# Approach\neach region can be uniquely identified by its top left element.\n1. we will iterate over all the possible top left element of region.\n2. check in 3*3 matrix if any adjacent element difference is not greater than thresold.\n3. if Not then return the sum of element. other wise return -1;\n4. we have created a 2d vector of pairs that will store the sum of region avg and no of regions as a pair.\n5. now at the end we wil avg out the averages of region that current index is part of.\n\n\n\n\n# Complexity\n- Time complexity:\n- N*M*9\n- Space complexity:\n 2* N * M\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>>dirn= {{1,0},{0,1},{-1,0},{0,-1}};\n    int isRegion(vector<vector<int>>& image, int threshold,int p,int q){\n        int sum=0;\n        for(int i=p;i<p+3;i++){\n            for(int j=q;j<q+3;j++){\n                for(auto &dir:dirn){\n                    int x= i+dir[0];\n                    int y= j+dir[1];\n                    if(x>=p&&x<(p+3) && y>=q && y<(q+3)){\n                        if(abs(image[i][j]-image[x][y])>threshold)\n                            return -1;\n                    }\n                }\n                sum+=image[i][j];\n            }\n        }\n        return sum;\n    }\n    void appendAvg(vector<vector<pair<int,int>>>&lookup,int p,int q,int sum){\n        int avg= sum/9;\n        for(int i=p;i<p+3;i++){\n            for(int j=q;j<q+3;j++){\n                if( lookup[i][j].second==0){\n                    lookup[i][j].first=avg;\n                    lookup[i][j].second=1;\n                    \n                }\n                else{\n                     lookup[i][j].first+=avg;\n                lookup[i][j].second+=1;\n                }\n               \n            }\n        }\n        return;\n        \n        \n    }\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<pair<int,int>>>lookup(n,vector<pair<int,int>>(m,{0,0}));\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                lookup[i][j]= {image[i][j],0};\n            }\n        }\n        for(int i=0;i+2<n;i++){\n            for(int j=0;j+2<m;j++){\n                // have to check for region;\n                int  sum = isRegion(image,threshold,i,j);\n                if(sum!=-1){ // we have found region\n\n                    appendAvg(lookup,i,j,sum);\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(lookup[i][j].second==0) res[i][j]= (lookup[i][j].first);\n                else res[i][j]= (lookup[i][j].first)/lookup[i][j].second;\n            }\n        }\n        \n        return res;\n        \n        \n        \n        \n    }\n};\n```
['C++']