Simple structured brute force C++
Intuition\nSimple structured brute force C++\n\n# Approach\nLoop through inside elements of the image matrix and not the boundary elements as boundary elements 
# Intuition\nSimple structured brute force C++\n\n# Approach\nLoop through inside elements of the image matrix and not the boundary elements as boundary elements will be check when considering 3x3 grid formed by inner elements.We choose inner elements for easy indexing of 3x3 matrix.isgridaround checks whether 3x3 grid can be formed for the centre elements as per the given condition.sum of elements is added in result matrix for each grid the element is in and the count of the grids the elements has is stored in another matrix later on we divide them to store the avg in result.\n\n\n# Complexity\n- Time complexity:\nO(NM)\n\n- Space complexity:\nO(NM)\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int rows = image.size();\n        int cols = image[0].size();\n        vector<vector<int>> result(rows, vector<int>(cols, 0));\n        vector<vector<int>> count(rows, vector<int>(cols, 0));\n        for(int i=1;i<image.size()-1;i++)\n        {for(int j=1;j<image[0].size()-1;j++)\n        {if(isgridaround(image[i][j],image,threshold,i,j))\n        {int avg=image[i][j];\n        avg+=image[i-1][j-1]+image[i+1][j+1]+image[i+1][j-1]+image[i-1][j+1]+image[i+1][j]+image[i-1][j]+image[i][j+1]+image[i][j-1];\n        avg=avg/9;\n        result[i][j]+=avg;\n        result[i-1][j-1]+=avg;\n        result[i+1][j+1]+=avg;\n        result[i+1][j-1]+=avg;\n        result[i-1][j+1]+=avg;\n        result[i+1][j]+=avg;\n        result[i-1][j]+=avg;\n        result[i][j+1]+=avg;\n        result[i][j-1]+=avg;\n        \n        count[i][j]++; \n        count[i-1][j-1]++;\n        count[i+1][j+1]++;\n        count[i+1][j-1]++;\n        count[i-1][j+1]++;\n        count[i+1][j]++;\n        count[i-1][j]++;\n        count[i][j+1]++;\n        count[i][j-1]++;}}}\n         for(int i=0;i<image.size();i++)\n        {for(int j=0;j<image[0].size();j++)\n        {if(count[i][j]==0)\n        {result[i][j]=image[i][j];\n        }}}\n        for(int i=0;i<image.size();i++)\n        {for(int j=0;j<image[0].size();j++)\n        {if(count[i][j]>0)\n        {result[i][j]=result[i][j]/count[i][j];}}}\n        return result;\n    }\n    \n    bool isgridaround(int key,vector<vector<int>>&image,int &threshold,int i,int j)\n    {bool ans=true;\n    if(abs(key-image[i+1][j])>threshold || abs(key-image[i-1][j])>threshold || abs(key-image[i][j+1])>threshold || abs(key-image[i][j-1])>threshold)\n    {return false;}\n    if(abs(image[i-1][j]-image[i-1][j+1])>threshold || abs(image[i-1][j]-image[i-1][j-1])>threshold|| abs(image[i+1][j]-image[i+1][j-1])>threshold|| abs(image[i+1][j]-image[i+1][j+1])>threshold|| abs(image[i][j+1]-image[i+1][j+1])>threshold||abs(image[i][j+1]-image[i-1][j+1])>threshold||abs(image[i][j-1]-image[i-1][j-1])>threshold||abs(image[i][j-1]-image[i+1][j-1])>threshold)\n    {return false;}\n    return ans;}\n};\n```
['C++']