3 ms || Beats 96% || Simple Traversal Logic || Self Explained Code || Well Explained Solution
My Other 3 Part\'s Solution\n\n- Spiral Matrix - I\n- Spiral Matrix - II\n- Spiral Matrix - IV\n\n# Intuition\n\n- Traversal Logic in a Grid\n\n# Approach\n\n- 
# My Other 3 Part\'s Solution\n\n- [Spiral Matrix - I](https://leetcode.com/problems/spiral-matrix/solutions/4877315/0-ms-beats-100-layer-by-layer-traversal-self-explained-code-well-explained-solution)\n- [Spiral Matrix - II](https://leetcode.com/problems/spiral-matrix-ii/solutions/4877342/0-ms-beats-100-layer-by-layer-traversal-self-explained-code-well-explained-solution)\n- [Spiral Matrix - IV](https://leetcode.com/problems/spiral-matrix-iv/solutions/4877418/7-ms-beats-91-simple-traversal-logic-self-explained-code-well-explained-solution)\n\n# Intuition\n\n- Traversal Logic in a Grid\n\n# Approach\n\n- ```is_within_bound()``` Method :\n\n    - This method checks if a given position (```current_i``` , ```current_j```) is within the bounds of a matrix with ```R``` rows and ```C``` columns.\n    - Parameters :\n        - ```current_i``` : Current Row index.\n        - ```current_j``` : Current Column index.\n        - ```R``` : Number of rows in the matrix.\n        - ```C``` : Number of columns in the matrix.\n    - Returns ```true``` if the position is within bounds, ```false``` otherwise.\n\n- ```spiralMatrixIII()``` Method :\n\n    - This method generates coordinates representing the positions of elements visited in a spiral order starting from a given position (```starting_i``` , ```starting_j```).\n    - Parameters:\n        - ```R``` : Number of rows in the matrix.\n        - ```C``` : Number of columns in the matrix.\n        - ```starting_i``` : Starting row index.\n        - ```starting_j``` : Starting column index.\n    - Returns a 2D array (```answer_matrix```) where each row contains the coordinates (```i``` , ```j```) of the visited positions in the spiral order.\n\n- Approach:\n\n    - Initialize a 2D array ```answer_matrix``` to store the coordinates of visited positions. Its size is ```(R * C) x 2```.\n    - Use a loop to traverse in a spiral order.\n    - For each iteration, move right, down, left, and up, adding coordinates to ```answer_matrix```.\n    - Increment ```elements_count``` after completing each spiral cycle to adjust the number of elements to be traversed.\n    - Return ```answer_matrix``` containing all the visited coordinates.\n\n# Complexity\n\n- Time complexity : $$O(R * C)$$\n\n- Space complexity : $$O(R * C)$$\n\n# Code\n\n```\nclass Solution {\n\n    public boolean is_within_bound(int current_i , int current_j , int R , int C){\n\n        return ((current_i >= 0) && (current_i < R) && (current_j >= 0) && (current_j < C));\n    }\n\n    public int[][] spiralMatrixIII(int R , int C , int starting_i , int starting_j){\n\n        int[][] answer_matrix = new int[(R * C)][2];\n\n        for(int current_i = starting_i , current_j = starting_j , elements_count = 1 , index = 0 ; (index < (R * C)) ; ){\n\n            for(int count = 1 ; (count <= elements_count) ; current_j++ , count++){\n\n                if(is_within_bound(current_i , current_j , R , C)){\n\n                    answer_matrix[index][0] = current_i;\n\n                    answer_matrix[index][1] = current_j;\n\n                    index++;\n                }\n            }\n\n            for(int count = 1 ; (count <= elements_count) ; current_i++ , count++){\n\n                if(is_within_bound(current_i , current_j , R , C)){\n\n                    answer_matrix[index][0] = current_i;\n\n                    answer_matrix[index][1] = current_j;\n\n                    index++;\n                }\n            }\n\n            elements_count++;\n\n            for(int count = 1 ; (count <= elements_count) ; current_j-- , count++){\n\n                if(is_within_bound(current_i , current_j , R , C)){\n\n                    answer_matrix[index][0] = current_i;\n\n                    answer_matrix[index][1] = current_j;\n\n                    index++;\n                }\n            }\n\n            for(int count = 1 ; (count <= elements_count) ; current_i-- , count++){\n\n                if(is_within_bound(current_i , current_j , R , C)){\n\n                    answer_matrix[index][0] = current_i;\n\n                    answer_matrix[index][1] = current_j;\n\n                    index++;\n                }\n            }\n\n            elements_count++;\n        }\n\n        return answer_matrix;\n    }\n}\n```
['Array', 'Matrix', 'Simulation', 'Java']