EASY SOLUTION.. direct and simple implementation|| JAVA
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe problem involves filling a matrix in a spiral order starting from a given coordin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe problem involves filling a matrix in a spiral order starting from a given coordinate. The approach can be visualized by imagining a spiral expanding from the starting point and ensuring that each step is taken only within the bounds of the matrix.\n\n## Key Observations:\n## Movement in Steps:\n\n## 1. \nThe movement pattern is right, down, left, and up.\n\n## 2. \nThe number of steps taken in each direction increases by 1 after every two directions (e.g., right and down, then left and up).\nBounds Checking:\n\n## 3. \nAt each step, we check if the new position is within the matrix bounds before adding it to the result list.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n## 1. Initialization:\n\nCreate a result array ans to store the coordinates in the spiral order.\nStart from the initial position (rStart, cStart) and add it to ans.\n\n## 2. Loop until all cells are visited:\n\nUse a variable x to keep track of the number of steps to take in the current direction.\nAlternate directions based on the parity of x (odd/even).\n## 3. Directions in the Spiral:\n\nOdd x: Move right x steps and then down x steps.\nEven x: Move left x steps and then up x steps.\n\n## 4. Steps in Each Direction:\n\nFor each direction, move one step at a time.\nAfter each move, check if the new position is within bounds. If yes, add it to ans.\n\n## 5.\nIncrement x after completing the moves in both directions (right-down or left-up).\n\n# Complexity\n- Time complexity:O(rows*cols)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(rows*cols)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int[][] ans = new int[rows * cols][2];\n        int index = 0;\n        \n        ans[index++] = new int[]{rStart, cStart};\n        \n        int rl = rStart, cl = cStart;\n        int x = 1;\n        \n        while (index < rows * cols) {\n            if (x % 2 == 1) {\n                for (int i = 0; i < x; i++) {\n                    cl++;\n                    if (cl < cols && cl >= 0 && rl < rows && rl >= 0) {\n                        ans[index++] = new int[]{rl, cl};\n                    }\n                }\n                for (int i = 0; i < x; i++) {\n                    rl++;\n                    if (rl < rows && rl >= 0 && cl < cols && cl >= 0) {\n                        ans[index++] = new int[]{rl, cl};\n                    }\n                }\n            } else {\n                for (int i = 0; i < x; i++) {\n                    cl--;\n                    if (cl < cols && cl >= 0 && rl < rows && rl >= 0) {\n                        ans[index++] = new int[]{rl, cl};\n                    }\n                }\n                for (int i = 0; i < x; i++) {\n                    rl--;\n                    if (rl < rows && rl >= 0 && cl < cols && cl >= 0) {\n                        ans[index++] = new int[]{rl, cl};\n                    }\n                }\n            }\n            x++;\n        }\n        \n        return ans;\n    }\n}\n\n```
['Java']