✅BEATS 100% | ✅SUPER EASY | ✅CLEAN, CONCISE, OPTIMIZED | ✅MULTI LANG | ✅CODING MADE FUN
Submission SS :\n\n\n\n# Intuition : Sliding Window\n\n# Approach\nHere\'s a structured and concise breakdown:\n\n1. Calculate Size of Resulting 2D Array (ans):
# Submission SS :\n![image.png](https://assets.leetcode.com/users/images/7af1928d-e9f6-4388-8ea6-8b151a0954b8_1715489548.6282287.png)\n\n\n# Intuition : Sliding Window\n\n# Approach\nHere\'s a structured and concise breakdown:\n\n1. **Calculate Size of Resulting 2D Array (`ans`)**:\n   - Subtract 2 from the length of the input grid (`n`) to determine the size of the resulting array (`m`).\n   - Initialize a 2D array `ans` with dimensions `(m) x (m)`.\n\n2. **Iterate Over Each Position in `ans`**:\n   - Iterate from `(0, 0)` to `(m-1, m-1)`.\n   - For each position `(i, j)` in `ans`, find the largest value in the corresponding 3x3 region of the input grid.\n\n3. **Find Largest Value in Each 3x3 Region**:\n   - Call the `largestLocalUtil` method for each position `(i, j)` in `ans`.\n   - In the `largestLocalUtil` method:\n     - Initialize a variable `max` to 0.\n     - Iterate over a 3x3 grid starting from position `(i, j)`.\n     - Update `max` to hold the maximum value found in the region.\n     - Return the maximum value found in the 3x3 region (`max`).\n\n4. **Store Maximum Values in `ans`**:\n   - Populate `ans` with the maximum values found in each local 3x3 region of the input grid.\n\n5. **Return Resulting 2D Array `ans`**:\n   - `ans` contains the largest values in each local 3x3 region of the input grid.\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n\n- Space complexity: $$O((n-2)^2)$$ ~ $$O(n^2)$$, \n\n# Code\n``` Java []\nclass Solution {\n\n    public int largestLocalUtil(int[][] grid, int x, int y) {\n        int max = 0;\n        \n        for (int i = x ; i < x+3 ; i++) {\n            for (int j = y ; j < y+3 ; j++) {\n                max = Math.max(max, grid[i][j]);\n            }\n        }\n        \n        return max;\n    }\n    \n    public int[][] largestLocal(int[][] grid) {\n        int n = grid.length;\n        \n        int m = n-2;\n        \n        int[][] ans = new int[m][m];\n        \n        for (int i = 0 ; i < m ; i++) {\n            for (int j = 0 ; j < m ; j++) {\n                ans[i][j] = largestLocalUtil(grid, i, j);\n            }\n        }\n        \n        return ans;\n    }\n}\n```\n``` C++ []\nclass Solution {\n\npublic:\n\n    int largestLocalUtil(std::vector<std::vector<int>>& grid, int x, int y) {\n        int max = 0;\n        \n        for (int i = x ; i < x+3 ; i++) {\n            for (int j = y ; j < y+3 ; j++) {\n                max = std::max(max, grid[i][j]);\n            }\n        }\n        \n        return max;\n    }\n    \n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        int n = grid.size();\n        \n        int m = n-2;\n        \n        std::vector<std::vector<int>> ans(m, std::vector<int>(m, 0));\n        \n        for (int i = 0 ; i < m ; i++) {\n            for (int j = 0 ; j < m ; j++) {\n                ans[i][j] = largestLocalUtil(grid, i, j);\n            }\n        }\n        \n        return ans;\n    }\n};\n```\n``` Python []\nclass Solution:\n\n    def largestLocalUtil(self, grid, x, y):\n        max_val = 0\n        \n        for i in range(x, x+3):\n            for j in range(y, y+3):\n                max_val = max(max_val, grid[i][j])\n        \n        return max_val\n    \n    def largestLocal(self, grid):\n        n = len(grid)\n        m = n - 2\n        \n        ans = [[0] * m for _ in range(m)]\n        \n        for i in range(m):\n            for j in range(m):\n                ans[i][j] = self.largestLocalUtil(grid, i, j)\n        \n        return ans\n\n``` \n\n**Space Optimized Approach :**\n* One can do the space optimization in C++ and Python by modifying the given matrix and resizing to n-2.\n\n**Complexity**\n* Space complexity: $$O(1)$$\n``` C++ []\nclass Solution {\n\npublic:\n\n    int largestLocalUtil(vector<vector<int>>& grid, int x, int y) {\n        int curMax = 0;\n        \n        for (int i = x-1 ; i <= x+1 ; i++) {\n            for (int j = y-1 ; j <= y+1 ; j++) {\n                curMax = max(curMax, grid[i][j]);\n            }\n        }\n        \n        return curMax;\n    }\n    \n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        int n = grid.size();\n        \n        for (int i = 1 ; i < n-1 ; i++) {\n            for (int j = 1 ; j < n-1 ; j++) {\n                grid[i-1][j-1] = largestLocalUtil(grid, i, j);\n            }\n        }\n        \n        int m = n-2;\n        \n        grid.resize(m);\n        \n        for (int i = 0 ; i < m ; i++) grid[i].resize(m);\n        \n        return grid;\n    }\n};\n```\n``` Python []\nclass Solution(object):\n\n    def largestLocalUtil(self, grid, x, y):\n        cur_max = 0\n        \n        for i in range(x-1, x+2):\n            for j in range(y-1, y+2):\n                cur_max = max(cur_max, grid[i][j])\n                \n        return cur_max\n    \n    def largestLocal(self, grid):\n        n = len(grid)\n        \n        for i in range(1, n-1):\n            for j in range(1, n-1):\n                grid[i-1][j-1] = self.largestLocalUtil(grid, i, j)\n                \n        m = n-2\n        \n        grid = [row[:m] for row in grid[:m]]\n        \n        return grid\n        \n```\n\n![image.png](https://assets.leetcode.com/users/images/c66a0b04-a5c4-4633-93ed-e047371c6807_1715489713.4756744.png)\n\n
['Array', 'Math', 'C', 'Sliding Window', 'Matrix', 'Simulation', 'Python', 'Java', 'Python3']