Easy to understand approach(brute force)
Intuition\nWe are taking all the 3x3 subgrids that are possible and checking if these are the valid or not along with the sum of elements present in the subgrid
# Intuition\nWe are taking all the 3x3 subgrids that are possible and checking if these are the valid or not along with the sum of elements present in the subgrid.\n\n# Approach\nit is simple brute force approach in which we are making a grid which containing pair .Each pair have two values first one is for sum of intensities and another is for number of regions that elements has occurred.\n\n# Complexity\n- Time complexity:\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool checker(vector<vector<int>> &image,int threshold,int r,int c,int &sum)\n    {\n        int n=r+3;\n        int m=c+3;\n\n        for(int i=r;i<n;i++)\n        {\n            for(int j=c;j<m;j++)\n            {\n                //left\n                if(j-1>=c && abs(image[i][j]-image[i][j-1]) > threshold)\n                return false;\n                \n                //right\n                if(j+1<m && abs(image[i][j]-image[i][j+1])> threshold)\n                return false;\n\n                //up\n                if(i-1>=r && abs(image[i][j]-image[i-1][j])>threshold)\n                return false;\n\n                //down\n                if(i+1<n && abs(image[i][j] - image[i+1][j]) > threshold)\n                return false;\n\n                sum+=image[i][j];\n            \n            }\n        }\n        return true;\n    }\n\n\n\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n\n        int n=image.size();\n        int m=image[0].size();\n\n        vector<vector<pair<int,int>>> result(n,vector<pair<int,int>> (m,{0,0}) );\n\n        for(int i=0;i<n-2;i++)\n        {\n            for(int j=0 ;j<m-2;j++)\n            {\n                int sum=0;\n\n                if(checker(image,threshold,i,j,sum))\n                {\n                    int intensity=sum/9;\n                    for(int k=i;k<i+3;k++)\n                    {\n                        for(int l=j;l<j+3;l++)\n                        {\n                            \n                            result[k][l].first+=intensity;\n                            result[k][l].second+=1;\n                        }\n                    }\n                }\n            }\n        }\n\n        vector<vector<int>> ans(n,vector<int> (m,0));\n\n        for(int i=0;i<n;i++)\n        {\n            for(int j=0;j<m;j++)\n            {\n                int intensity=result[i][j].first;\n                int num=result[i][j].second;\n                if(intensity==0 && num==0)\n                {\n                    ans[i][j]=image[i][j]; \n                }\n                else\n                ans[i][j]=intensity/num;\n            }\n        }\n\n      return ans;\n\n    }\n};\n```
['C++']