Line by Line Explained || Easy to Understand || Pure Implemention
Intuition\n Describe your first thoughts on how to solve this problem. \njust do what the question is asking (Pure implemention )\n\n# Approach\n Describe your 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\njust do what the question is asking (Pure implemention )\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\ntake two 2-D vectors \'sum\' and \'cnt\' \nsum[i][j] : will store to avg sum of all regions where this cell belong\ncnt[i][j] : will store number of regions where this cell belong\nat the end \nif ( cnt[i][j] == 0 ) sum[i][j] = image[i][j] ;\nelse sum[i][j] /= cnt[i][j]; // avg of all avegrages\n\n# Complexity\n- Time complexity:\n- O((n*m)2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(n*m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size() , m = image[0].size();\n\n        vector<vector<int>>sum(n,vector<int>(m,0)); // sum of all avg values of region where this cell lies \n        vector<vector<int>>cnt(n,vector<int>(m,0)); // count of region where thi will lies\n        // answer will sum[i][j]/cnt[i][j] if cnt[i][j] != 0 else image[i][j]\n\n        // its a implementation based problem \n\n        // to chechk adjacent \n        int dr[] = {-1,0,1,0,0};\n        int dc[] = {0,1,0,-1,0};\n        for ( int r1 = 0 ; r1 < n-2 ; r1++ )\n        {\n            for ( int c1 = 0 ; c1 < m-2 ; c1++ )\n            {\n                bool ok = 1 ;\n                int pxlSum = 0 ; // it will hold sum of pixels in 3*3 grid\n                // traversing in 3*3 grid \n                for ( int r2 = r1 ; r2 <= r1 + 2 ; r2++ )\n                {\n                    for ( int c2 = c1 ; c2 <= c1 + 2 ; c2++ )\n                    {\n                        pxlSum += image[r2][c2];\n                        for ( int i = 0 ; i<5; i++ ) \n                        {\n                            int r = r2 + dr[i];\n                            int c = c2 + dc[i];\n                            if ( r>=r1 && r <= r1+2 && c>=c1 && c<=c1+2 )\n                            {\n                                if (  abs(image[r][c]-image[r2][c2]) > threshold ) ok = 0 ; // if any fails then don\'t consider this region\n                            }\n                        }\n                    }\n                }\n                if ( ok )\n                {\n                    for ( int r2 = r1 ; r2 <= r1+2 ; r2++)\n                    {\n                        for ( int c2 = c1 ; c2 <= c1+2 ; c2++ )\n                        {\n                            sum[r2][c2] += pxlSum/9;\n                            cnt[r2][c2]++;\n                        }\n                    }\n                }\n            }\n        }\n\n        for ( int i = 0; i<n; i++ )\n        {\n            for ( int j = 0; j<m; j++ )\n            {\n                if ( cnt[i][j] == 0  ) sum[i][j] = image[i][j];\n                else sum[i][j]/=cnt[i][j];\n            }\n        }\n        return sum ;\n    }\n\n// Upvote : if this makes u understand \n};\n```
['C++']