✅ C++ One-Loop Easy Solution | Column-first and Row-first Approaches
There\'s nothing much to this problem - Just check if total elements in both matrices will be same and then transform. I have mentioned two approaches below.\n\
There\'s nothing much to this problem - Just check if total elements in both matrices will be same and then transform. I have mentioned two approaches below.\n\n----\n\n\u2714\uFE0F ***Solution (Row-First Approach)***\n\nIterate each row column-by-column, wrap around when you reach the end on one row and move to the next row. Here, we are copying all elements of one row and then moving on to the next row.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int m = size(mat), n = size(mat[0]), total = m * n;\n        if(r * c != total) return mat;\n        vector<vector<int>> ans(r, vector<int>(c));\n        for(int i = 0; i < total; i++) \n            ans[i / c][i % c] = mat[i / n][i % n];\n        return ans;\n    }\n};\n```\n\n<blockquote>\n<details>\n<summary><b>\u2714\uFE0F Commented Solution</b></summary>\n\n\nIf the above appraoch was not clear, you can refer the below commented code -\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int m = size(mat), n = size(mat[0]), total = m * n;\n        if(r * c != total) return mat;             // return if total elements don\'t match\n        vector<vector<int>> ans(r, vector<int>(c));\n        int i = 0, j = 0, new_i = 0, new_j = 0;    // i, j iterates over old matrix | new_i, new_j iterates over new matrix\n        while(total--) {\n            ans[new_i][new_j] = mat[i][j]; \n            j++, new_j++;                          // move to next column in both matrix\n            if(j == n) j = 0, i++;                 // start from beginning of next row when you reach the end of row in old matrix\n            if(new_j == c) new_j = 0, new_i++;     // start from beginning of next row when you reach the end of row in new matrix\n        }\n        return ans;\n    }\n};\n```\n\n</details>\n</blockquote>\n\n***Time Complexity :*** `O(r*c)`\n***Space Complexity :*** `O(1)`, ignoring output space complexity\n\n\n---\n\n\u2714\uFE0F ***Solution - II (Column-First Appraoch)***\n\nAll other posts have mentioned the first approach. It might be a good follow-up to try implementing it using column-first approach.\n\nIterate each column row-by-row, wrap around when you reach the end on one column and then move to the next column. Here, we are copying all elements of one column and then moving on to the next column.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int m = size(mat), n = size(mat[0]), total = m * n;\n        if(r * c != total) return mat;   \n        vector<vector<int>> ans(r, vector<int>(c));\n        for(int col = 0; col < n; col++) {\n            for(int row = 0; row < m; row++) {\n                int new_row = (n * row + col) / c;\n                int new_col = (n * row + col) % c;\n                ans[new_row][new_col] = mat[row][col];                \n            }\n        }\n        return ans;\n    }\n};\n```\n\n<blockquote>\n<details>\n<summary><b>\u2714\uFE0F Single-Loop Solution</b></summary>\n\nIf anyone figures out a cleaner code than the following version, please let me know :)\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int m = size(mat), n = size(mat[0]), total = m * n;\n        if(r * c != total) return mat;   \n        vector<vector<int>> ans(r, vector<int>(c));\n        for(int i = 0; i < total; i++) \n            ans[(i%m * n + i/m) / c][(i%m * n + i/m) % c] = mat[i % m][i / m];\n        return ans;\n    }\n};\n```\n\n\n</details>\n</blockquote>\n\n***Time Complexity :*** `O(r*c)`\n***Space Complexity :*** `O(1)`, ignoring output space complexity\n\n---\n---\n\n\uD83D\uDCBB\uD83D\uDC31\u200D\uD83D\uDCBBIf there are any suggestions / questions / mistakes in my post, comment below \uD83D\uDC47 \n\n---\n---
['C']