C++ solution with explanation and complexity
After solving a few examples by myself, I found some observations:\n The number of grids that we are traversing in one direction starts from 1 and after travers
After solving a few examples by myself, I found some observations:\n* The number of grids that we are traversing in one direction starts from 1 and after traversing two direction it increases. I have called it as **checker** in code\n* For stoping the traversal, we have to count all rows * cols grids. So i created **counter**\n* Before pushing the coordinates in the ans vector, we need to check if it belongs inside the matrix or not. For that \n\t\t0 <= i <= rows\n\t\t0 <= j <= cols\nwhere i is the index for row and j is the index for column.\n\n\n\n![image](https://assets.leetcode.com/users/images/ea296c0e-0175-4170-816b-133d40d94c51_1667559130.6659496.jpeg)\n\n**Space Complexity**: O(rows * cols)\n**Time Complexity:** O(6 + (n - 1)* 8), where n = max(rows, cols)\n```\nclass Solution {\npublic:\n\n\t//function to check if i and j belong to matrix or not\n    bool valid_to_push(int i, int j, int rows, int cols){\n        if(i>=0 && i<rows && j>=0 && j<cols)\n            return true;\n        return false;\n    }\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        vector<vector<int>> ans;\n        int counter, direction, checker;\n        ans.push_back({rStart, cStart});\n        counter = 1; \n        direction = 0;\n        checker = 1;\n        \n        int i = rStart, j = cStart;\n\t\t\n        while(counter != rows * cols){\n\t\t\t// for direction 0\n            if(direction == 0){\n\t\t\t\t// only traverse till checker\n                for(int k = 1; k<= checker; k++){\n                    j++;\n                    if(valid_to_push(i,j,rows,cols)){\n                        ans.push_back({i, j});\n                        counter++;\n                    }\n                }\n                direction = 1; //change direction\n            }\n\t\t\t// for direction 1\n            if(direction == 1){\n                for(int k = 1; k<= checker; k++){\n                    i++;\n                    if(valid_to_push(i,j,rows,cols)){\n                        ans.push_back({i, j});\n                        counter++;\n                    }\n                }\n                direction = 2; //change direction\n            }\n            checker++; // increase after traversing two direction\n\t\t\t\n\t\t\t// for direction 2\n            if(direction == 2){\n                for(int k = 1; k<= checker; k++){\n                    j--;\n                    if(valid_to_push(i,j,rows,cols)){\n                        ans.push_back({i, j});\n                        counter++;\n                    }\n                }\n                direction = 3; //change direction\n            }\n\t\t\t\n\t\t\t// for direction 3\n            if(direction == 3){\n                for(int k = 1; k<= checker; k++){\n                    i--;\n                    if(valid_to_push(i,j,rows,cols)){\n                        ans.push_back({i, j});\n                        counter++;\n                    }\n                }\n                direction = 0; //change direction\n            }\n            checker++; // increase after traversing two direction\n        }\n        \n        return ans;\n    }\n};\n```
['C', 'C++']