Short simple C++ solution with explanation
Time Complexity: O(m x n), where m is no of rows, n is no of columns in inital matrix\nSpace Complexity: O(1)\n\nIntution: Consider a 2D matrix arr of size m x 
**Time Complexity:** O(m x n), where m is no of rows, n is no of columns in inital matrix\n**Space Complexity:** O(1)\n\n**Intution:** Consider a 2D matrix arr of size m x n, and a counter, let\'s say **t** to count the elements linearly and using it we can visualise this 2D matrix as 1D matrix. Now to convert it back to 2D matrix (which will be the solution) we can use this formula:\n\n**arr[i][j]** = **arr[t/no_of_columns][t%no_of_columns]**, where i, j points to particular valid index\n\nand it will map to the required index in the solution matirx. Try with 3-4 2D matrix of different sizes and it will become more clear.\n\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n//      count of rows & columns of initial matrix \n        int m = mat.size(), n = mat[0].size();\n        \n//      Checking if matrix can be reshaped or not\n        if ((m*n) != (r*c)) return mat;\n        \n//      pointer to insert values in the new / reshaped matrix\n        int t = 0;\n        \n//      vector to store the new matrix\n        vector<vector<int>> res(r, vector<int> (c)); \n        \n        for(int i = 0; i < m; i++){\n            for(int j = 0; j < n; j++){\n                res[t/c][t%c] = mat[i][j];\n                t++;\n            }\n        }\n        return res;\n    }\n};\n```\n\nIf you liked the solution, please consider upvoting.
['Array', 'C']