✅ Reshape the Matrix | Simple Solution
Solution:(Accepted)\n\nAs the question is very easy to follow but we need to keep these condition in mind,\n1) If number of elements in current matrix != r * c 
***Solution:(Accepted)***\n```\nAs the question is very easy to follow but we need to keep these condition in mind,\n1) If number of elements in current matrix != r * c (Invalid case) then we have to return the current matrix.\n2) we will take two iterators to fill out new matrix ,\n\tRow iterator: ri\n\tCol iterator: ci\n3) if at any point our ci==c (It means we successfully covered the row), then we have to increment our row counter,\n\ti.e, ri++,\n\t--> As for the next row ci should start with 0 , so we make ci=0 also,\n4) We follow the same process (step 2 and 3) until every element is added to new matrix.\n\nPlease find the solution below:\n```\n**C++:**\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) \n    {\n        int m=mat.size();\n        int n=mat[0].size();\n        if(m*n!=r*c)\n            return mat;\n        vector<vector<int>> res(r);\n        int ri=0,ci=0;\n        for(int i=0;i<m;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(ci==c)\n                {\n                    ri=ri+1;\n                    ci=0;\n                }\n                res[ri].push_back(mat[i][j]);\n                ci=ci+1;\n            }\n        }\n        return res;\n    }\n};\n```\n***\n**if you found the post useful then do consider it for Upvotes :), Happy coding**\n***\n\n
[]