JAVA Spiral Matrix III
In this question first we need to observe ans understand that how the direction will change or I should say what is the condition when the iterator itr (which i
In this question first we need to observe ans understand that how the direction will change or I should say what is the condition when the iterator itr (which is traversing array(matrix)) will change its direction of iteration.\n\nBy observation we figured it out that iterator itr is traversing the array in a pattern or elements of array are arranged in a definite pattern \n   itr moves 1 step in right\n   itr then moves 1step down \n   itr then moves 2 steps in left\n   itr then moves 2 steps top\n   itr then moves 3 steps in right\n   itr then moves 3 steps down\n   itr then moves 4 steps in left\n   itr then moves 4 steps top\n   itr then moves 5 step in right\n   .\n   .\n   .\n   .\n   .\n   .\n   .\n   .\n   ans so on till the last element is reached\n   \n  ![image](https://assets.leetcode.com/users/images/cf928875-778b-49d7-9fc0-c5e08c1e17de_1656001345.1119683.png)\n\n\n   \n   here we can observe that itreator itr traverse the array in a pattern   1,1,2,2,3,3,4,4,5,5,6,6,.............\n   \n   so now we know the pattern, we have to derive the formula to get that pattern to traverse the array\n   let  A(0)==1, A(1)==1, A(2)==2, A(3)==2, A(4)==3, A(5)==3, A(6)==4, . . . . . . . . . . . . . . and so on\n   \n   so, by this we can conclude\n        A(n)= n/2 + 1;\n\t\there n belong to (0, length of row of ans)\n\t\t\n\t\t\n\t\t\n   Now, we have one more challenge to change the direction of array in this we have four cases\n  Case 1:-  When iterate from left to right\n\t\t\t\t  here we need to increment the column index only \n\n  Case 2:- When iterate from top to bottom\n\t\t\t\there we need to increment the row index only\n\n  Case 3:- When iterate from right to left\n\t\t\t\there we need to decrement the column index only\n\n  Case 4:- When iterate from bottom to top \n\t\t\t\there we need to decrement the row index only\n\t\t\t\t\t\nTo handle or perform the above for cases we can do this by using simple technique\ntake   int dx=0, dy=1, temp=0;\nand    rStart==x, cStart=y\n\nnow just perfor this simple following code when the inner loop has done traversing in one direction or  you can say after  the completion of inner loop \n\ntemp=dx;\ndx=dy;\ndy= -tmp;\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t\n\t\t```\n\t\t\n\t\tclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int x, int y) {\n        \n        int[][] ans=new int[rows*cols][2];\n        int n=0;\n        int dx=0;\n        int dy=1;\n        int temp=0;\n        \n        for(int j=0;j<rows*cols;++n){\n            for(int i=0;i< n/2 + 1;++i){\n                \n                if(x>=0 && x<rows && y>=0 && y<cols){\n                    // here we are assigning the new 1D array to row of 2D array (ans) \n                    // it will remind you some basic concept of aray that every thing in java is an object\n                    ans[j++]=new int[]{x,y};\n                }\n                \n                x=x+dx;\n                y=y+dy;\n            }\n            // by this code we will change the direction of array(ans) traversing\n            // left to right\n            // top to bottom\n            // right to left\n            // bottom to up\n            temp=dx;\n            dx=dy;\n            dy= -temp;\n        }\n        \n        return ans;\n    }\n}\n\n```\n\t\t\n
['Matrix']