Brute Force || Simple Solution || C++
Intuition\nWe consider a 3x3 region around each pixel, and if the absolute difference in intensity between any two adjacent pixels in this region exceeds the th
# Intuition\nWe consider a 3x3 region around each pixel, and if the absolute difference in intensity between any two adjacent pixels in this region exceeds the threshold, we skip altering that central pixel\'s intensity. Otherwise, we average the intensities of the pixels in the region to determine the new intensity of the central pixel.\n\n# Approach\nThe algorithm processes each 3x3 region of the image as follows:\n\n1. Calculate the sum of the pixel intensities within the region.\n2. Traverse each pixel in the region, comparing it against its neighbors to confirm that intensity differences stay within the specified threshold.\n3. If all differences are within the threshold, compute the average intensity for the region and update the central pixel\'s intensity accordingly.\n4. Maintain two matrices: `result` for the cumulative intensities and `count` for the number of updates per pixel.\n\nAfter processing all regions, iterate over the entire image one more time to finalize each pixel\'s intensity using the `result` and `count` matrices.\n\n\n# Complexity\n- Time complexity:\n$$O(mn)$$ \u2014 Here, `m` is the number of rows and `n` is the number of columns in the image. Each pixel is visited once, and a constant amount of work is done for each, given the fixed size of the region.\n\n- Space complexity:\n$$O(mn)$$ \u2014 This accounts for the additional space taken up by the `result` and `count` matrices, which are of the same dimensions as the input image.\n\n# Code\n```\nclass Solution {\npublic:\n    void checkregion(int x1,int y1,vector<vector<int>>& image, int threshold,vector<vector<int>>&result,vector<vector<int>>&count){\n        int m=image.size();\n        int n=image[0].size();\n        int dx[4]={1,-1,0,0};\n        int dy[4]={0,0,1,-1};\n        int x2=x1+2;\n        int y2=y1+2;\n        int sum=0;\n        for(int i=x1;i<=x2;i++){\n            for(int j=y1;j<=y2;j++){\n                sum+=image[i][j];\n                for(int k=0;k<4;k++){\n                    int nx=i+dx[k];\n                    int ny=j+dy[k];\n                    \n                    if(nx>=x1 && nx<=x2 && ny>=y1 && ny<=y2){\n                        if(abs(image[i][j]-image[nx][ny])>threshold){\n                            return;\n                        }\n                        \n                    }\n                }\n            }\n        }\n        cout<<sum/9;\n        for(int i=x1;i<=x2;i++){\n            for(int j=y1;j<=y2;j++){\n                count[i][j]++;\n                result[i][j]+=sum/9;\n            }\n        }\n        \n    }\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        \n        int m=image.size();\n        int n=image[0].size();\n        vector<vector<int>> result(m,vector<int>(n,0));\n        vector<vector<int>> count(m,vector<int>(n,0));\n        \n        for(int i =0;i<m-2;i++){\n            for(int j=0; j<n-2;j++){\n                checkregion(i,j,image,threshold,result,count);       \n            }\n        }\n        \n        \n        for(int i =0;i<m;i++){\n            for(int j=0; j<n;j++){\n                if(count[i][j]>0){\n                    result[i][j]=result[i][j]/count[i][j];\n                }\n                else{\n                    result[i][j]=image[i][j];\n                }\n            }\n        }\n        \n        return result;\n        \n    }\n};\n```
['C++']