Spiral Mtrix-III (Unique solution - Easy explanation) - Time and space complexity in O(rows*cols)
Intuition\n Describe your first thoughts on how to solve this problem. \nTo travel in spiral direction in the matrix,traverse in right(increment the column),the
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo travel in spiral direction in the matrix,traverse in right(increment the column),then traverse in bottom(increment the  row),then traverse in left(decrement the column) and traverse in the up(decrement the row).And in each traversal,the step may increase as the spiral goes large.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Declare an array matrix(In our case : ans) to store the pair in the order in which they traverse in ans matrix,So it may hold rows*cols pairs.\n\n- Then declare and initialize the directions array for one iteration of spiral movement,{0,1} - traverse in right, {1,0} - traverse in bottom, {0,-1} - traverse in left, {-1,0} - traverse in up.\n\n- For initial spiral,the step must be 1.Hence initialize steps to 1,then traverse the loop from 0 to rows*cols.\n\n- Then take each pair of direction separately and repeat the loop steps number of times.\n\n- Then add the pair\'s 0-th index to row r and 1-st index to col c,then check if r is in the range of 0 to rows and c is in the range of 0 to cols.\n\n- If so,add that pair to the ans array.\n\n- And check if k is greater than rows*cols ,if so - return the ans array.\n\n- If the pairs first index is not equal to 0,then increment the steps is incremented in each step.\n\n- Then return the ans index which contains the path of the spiral in the matrix.\n\n# Complexity\n- Time complexity:**O(rows * cols)**\n\n  **while loop** : traversed until all the cells in the grid are visited.Since we are visiting every cells exactly once,the number of iteration is proportional to number of cells in the grid.\n\n  **Inside while loop** :Within the while loop, each cell is processed exactly once, and each direction is traversed step by step, ensuring we visit each cell systematically in a spiral order.\n\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**O(rows * cols)**\n\n   **Primary source usage**:  comes from ans array which stores co-ordinates of all cells in the grid.The size of the array is rows*cols to size all the co-ordinates.\n\n  **Additional space**:  is used by the directions array and a few variables, but this space is negligible compared to the space used by the ans array. Thus, the overall space complexity is dominated by the ans array.\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 k = 0;\n        \n        // Directions represent right, down, left, up in terms of row and column changes\n        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n        \n        // Starting position\n        int r = rStart, c = cStart;\n        \n        ans[k++] = new int[]{r, c};\n        \n        int steps = 1;\n        int len=rows*cols;\n        while (k < len) {\n            for (int[] direction : directions) {\n                for (int i = 0; i < steps; i++) {\n                    r += direction[0];\n                    c += direction[1];\n                    \n                    if (r >= 0 && r < rows && c >= 0 && c < cols) {\n                        ans[k++] = new int[]{r, c};\n                        if (k >= rows * cols) {\n                            return ans;\n                        }\n                    }\n                }\n                // Increment steps after every second direction (right and left)\n                if (direction[0] != 0) {\n                    steps++;\n                }\n            }\n        }\n        \n        return ans;\n    }\n}\n```
['Matrix', 'Java']