Easy Video Solution 🔥 || Implement the requirements given in the question ✅ || Simple Logic
Intuition\n Describe your first thoughts on how to solve this problem. \nUnderstand the question clearly and breakdown it into diffrent concepts. Then you can s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUnderstand the question clearly and breakdown it into diffrent concepts. Then you can see the logic coming from you \uD83D\uDD25\n\n# ***Easy Video Explanation***\n\nhttps://youtu.be/b1X4jbhDabs\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBasic\nThe code processes a 2D grid, calculates the average of each 3x3 subgrid excluding elements with differences greater than \'k\'. The results are stored in a new grid.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N*N*9)\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N*N)\n    \n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& v, int k) {\n        int r=v.size(),c=v[0].size();\n        map<pair<int,int>,pair<int,int>>m;\n        for(int i=0;i<r-2;i++) {\n            for(int j=0;j<c-2;j++) {\n                int s=0;\n                int f=0;\n                for(int x=i;x<i+3;x++) {\n                    for(int y=j;y<j+3;y++) {\n                        s=s+v[x][y];\n                        if(x+1<i+3) {\n                            if(abs(v[x][y]-v[x+1][y])>k) {\n                                f=1;\n                                break;\n                            }\n                        }\n                        if(y+1<j+3) {\n                            if(abs(v[x][y]-v[x][y+1])>k) {\n                                f=1;\n                                break;\n                            }\n                        }\n                            \n                    }\n                    if(f==1)\n                        break;\n                }\n                if(f==1) {\n                    continue;\n                }\n                s=s/9;\n                for(int x=i;x<i+3;x++) {\n                    for(int y=j;y<j+3;y++) {\n                        if(m.find({x,y})!=m.end()) {\n                            pair<int,int>p=m[{x,y}];\n                            m[{x,y}]={p.first+s,p.second+1};\n                        }\n                        else {\n                            m[{x,y}]={s,1};\n                        }\n                        \n                    }\n                }\n            }\n        }\n        \n        vector<vector<int>>ans(r,vector<int>(c,0));\n        for(int i=0;i<r;i++) {\n            for(int j=0;j<c;j++) {\n                if(m.find({i,j})==m.end()) {\n                    ans[i][j]=v[i][j];\n                    continue;\n                }\n                pair<int,int>p=m[{i,j}];\n                int s=p.first/p.second;\n                ans[i][j]=s;\n            }\n        }\n        return ans;\n    }\n};\n```\n```python []\nfrom collections import defaultdict\n\nclass Solution:\n    def resultGrid(self, v, k):\n        r, c = len(v), len(v[0])\n        m = defaultdict(lambda: [0, 0])\n\n        for i in range(r - 2):\n            for j in range(c - 2):\n                s, f = 0, 0\n\n                for x in range(i, i + 3):\n                    for y in range(j, j + 3):\n                        s += v[x][y]\n\n                        if x + 1 < i + 3 and abs(v[x][y] - v[x + 1][y]) > k:\n                            f = 1\n                            break\n\n                        if y + 1 < j + 3 and abs(v[x][y] - v[x][y + 1]) > k:\n                            f = 1\n                            break\n\n                    if f == 1:\n                        break\n\n                if f == 1:\n                    continue\n\n                s = s // 9\n\n                for x in range(i, i + 3):\n                    for y in range(j, j + 3):\n                        m[x, y][0] += s\n                        m[x, y][1] += 1\n\n        ans = [[0] * c for _ in range(r)]\n\n        for i in range(r):\n            for j in range(c):\n                if (i, j) in m:\n                    s, count = m[i, j]\n                    ans[i][j] = s // count\n                else:\n                    ans[i][j] = v[i][j]\n\n        return ans\n\n```\n```Java []\nimport java.util.*;\n\npublic class Solution {\n    public ArrayList<ArrayList<Integer>> resultGrid(ArrayList<ArrayList<Integer>> v, int k) {\n        int r = v.size(), c = v.get(0).size();\n        HashMap<Pair<Integer, Integer>, Pair<Integer, Integer>> m = new HashMap<>();\n\n        for (int i = 0; i < r - 2; i++) {\n            for (int j = 0; j < c - 2; j++) {\n                int s = 0;\n                int f = 0;\n\n                for (int x = i; x < i + 3; x++) {\n                    for (int y = j; y < j + 3; y++) {\n                        s += v.get(x).get(y);\n\n                        if (x + 1 < i + 3 && Math.abs(v.get(x).get(y) - v.get(x + 1).get(y)) > k) {\n                            f = 1;\n                            break;\n                        }\n\n                        if (y + 1 < j + 3 && Math.abs(v.get(x).get(y) - v.get(x).get(y + 1)) > k) {\n                            f = 1;\n                            break;\n                        }\n                    }\n                    if (f == 1)\n                        break;\n                }\n\n                if (f == 1)\n                    continue;\n\n                s = s / 9;\n\n                for (int x = i; x < i + 3; x++) {\n                    for (int y = j; y < j + 3; y++) {\n                        Pair<Integer, Integer> key = new Pair<>(x, y);\n\n                        if (m.containsKey(key)) {\n                            Pair<Integer, Integer> p = m.get(key);\n                            m.put(key, new Pair<>(p.getKey() + s, p.getValue() + 1));\n                        } else {\n                            m.put(key, new Pair<>(s, 1));\n                        }\n                    }\n                }\n            }\n        }\n\n        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();\n\n        for (int i = 0; i < r; i++) {\n            ArrayList<Integer> row = new ArrayList<>();\n\n            for (int j = 0; j < c; j++) {\n                Pair<Integer, Integer> key = new Pair<>(i, j);\n\n                if (!m.containsKey(key)) {\n                    row.add(v.get(i).get(j));\n                    continue;\n                }\n\n                Pair<Integer, Integer> p = m.get(key);\n                int s = p.getKey() / p.getValue();\n                row.add(s);\n            }\n\n            ans.add(row);\n        }\n\n        return ans;\n    }\n}\n\n\n```\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you* \uD83D\uDE00
['Ordered Map', 'C++', 'Java', 'Python3']