C++ Simple Soln
Intuition\n Describe your first thoughts on how to solve this problem. \nTraversing spirally and checking whether that element belong to matrix or not\n# Approa
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTraversing spirally and checking whether that element belong to matrix or not\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe are traversing the matrix spirally and for each element we are checking  whether it lies in matrix or not and if len of answer becomes equal to rows*cols, we return.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(rows*rows*cols*cols)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n    bool isvalid(int i,int j, int row, int col)\n    {\n        return (i>=0 && j>=0 && i<row && j<col);\n    }\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        vector<vector<int>> ans;\n        ans.push_back({rStart,cStart});\n\n        int step = 1;\n        int i = rStart,j = cStart;\n\n        while(ans.size() < rows*cols)\n        {\n            if(ans.size()==rows*cols)\n            return ans;\n\n            int k=0;\n            while(k<step)\n            {\n                j++;\n                if(isvalid(i,j,rows,cols))\n                {\n                    ans.push_back({i,j});\n                    if(ans.size()==rows*cols)\n                    return ans;\n                }\n                k++;\n            }\n\n            k=0;\n            while(k<step)\n            {\n                i++;\n                if(isvalid(i,j,rows,cols))\n                {\n                    ans.push_back({i,j});\n                    if(ans.size()==rows*cols)\n                    return ans;\n                }\n                k++;\n            }\n\n            step++;\n\n            k=0;\n            while(k<step)\n            {\n                j--;\n                if(isvalid(i,j,rows,cols))\n                {\n                    ans.push_back({i,j});\n                    if(ans.size()==rows*cols)\n                    return ans;\n                }\n                k++;\n            }\n\n            k=0;\n            while(k<step)\n            {\n                i--;\n                if(isvalid(i,j,rows,cols))\n                {\n                    ans.push_back({i,j});\n                    if(ans.size()==rows*cols)\n                    return ans;\n                }\n                k++;\n            }\n\n            step++;\n\n        }\n        return ans;\n    }\n};\n```
['C++']