Easy explaination | video solution | As simple as Possible | C++ | O(200*max(row,col))
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst of all look at the image given below:\n\n\nNow You can clearly see that for any g
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst of all look at the image given below:\n![Screenshot (176).png](https://assets.leetcode.com/users/images/5e83a4ab-8f08-49b1-a160-2baf5a8c542c_1723093442.7415159.png)\n\nNow You can clearly see that for any gap we have to traverse one time in row and one time in column.\nfor odd gap direction will be left to right and top to bottom respectively and similarly for even gap direction will be right to left and bottom to top.\n\nso I run a loog for gap=1 to gap=200 just for being at safe side.\nthen inside that loop i have individually handle the 4 loop for rows and cols for both direction.\nwe will include only that cell in answer which is on the grid so for that i am validating by using ```isValid``` function.\n# video solution\n[https://www.youtube.com/watch?v=pBq5t2ABWSY]()\nfor clear observation i suggest you to go through comments in code and dry run itself.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(200*max(row,col))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(row*col)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nbool isValid(int rows,int cols,int i,int j){\n    if(i>=0 && j>=0 && i<rows && j<cols) return true;\n    return false;\n}\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int gap=1; // denoting gap as seen in image\n        vector<vector<int>> ans;\n        while(gap<= 200){ // for safer side we go till 200\n            \n            for(int i=cStart;i<gap+cStart;i++){\n                if(isValid(rows,cols,rStart,i))\n                ans.push_back({rStart,i}); // traverse in row left to right\n            }\n            cStart= gap+cStart;\n            for(int i=rStart;i<gap+rStart;i++){\n                if(isValid(rows,cols,i,cStart)){\n                    ans.push_back({i,cStart}); // traverse in col from top to bottom side\n                }\n            }\n            rStart=rStart+gap;\n            gap++;  // increase gap as observe from image\n            for(int i=cStart;i>cStart-gap;i--){\n                if(isValid(rows,cols,rStart,i)){\n                    ans.push_back({rStart,i}); // traverse in row from right to left\n                }\n            }\n            cStart=cStart-gap;\n            for(int i=rStart;i>rStart-gap;i--){\n                if(isValid(rows,cols,i,cStart)){\n                    ans.push_back({i,cStart}); // traverse in col from bottom to top side\n                }\n            }\n            rStart= rStart-gap;\n            gap++;\n        }\n        return ans;\n    }\n};\n```
['C++']