Very Easy || 100% || Fully Explained || Java, C++, Python, JavaScript, Python3
Java Solution:\n\n// Runtime: 1 ms, faster than 92.33% of Java online submissions for Reshape the Matrix.\n// Time Complexity : O(r*c)\n// Space Complexity : O(
# **Java Solution:**\n```\n// Runtime: 1 ms, faster than 92.33% of Java online submissions for Reshape the Matrix.\n// Time Complexity : O(r*c)\n// Space Complexity : O(r*c)\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n        // If transformation doesn\'t occur, return mat...\n        if (mat.length * mat[0].length != r * c) {\n            return mat;\n        }\n        // Otherwise create a output matrix and fill the cells...\n        int[][] output = new int[r][c];\n        // Traverse the matrix through the loop... \n        for (int idx = 0; idx < r * c; idx++) {\n            // idx % c will give us the current column number...\n            // idx / c will give us how many rows we have completely filled...\n            output[idx/c][idx % c] = mat[idx / mat[0].length][idx % mat[0].length];\n        }\n        return output;      // Return the output matrix...\n    }\n}\n```\n\n# **C++ Solution:**\n```\n// Time Complexity : O(r*c)\n// Space Complexity : O(r*c)\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        // If transformation doesn\'t occur, return mat...\n        if (mat.size() * mat[0].size() != r * c) {\n            return mat;\n        }\n        // Otherwise create a output matrix and fill the cells...\n        vector<vector<int>> output(r, vector<int>(c));\n        // Traverse the matrix through the loop... \n        for (int idx = 0; idx < r * c; idx++) {\n            // idx % c will give us the current column number...\n            // idx / c will give us how many rows we have completely filled...\n            output[idx/c][idx % c] = mat[idx / mat[0].size()][idx % mat[0].size()];\n        }\n        return output;      // Return the output matrix...\n    }\n};\n```\n\n# **Python/Python3 Solution:**\n```\n# Time Complexity : O(r*c)\n# Space Complexity : O(r*c)\nclass Solution(object):\n    def matrixReshape(self, mat, r, c):\n        # Base case...\n        if not mat: return mat\n        # If transformation doesn\'t occur, return mat...\n        if len(mat) * len(mat[0]) != r * c:\n            return mat\n        # Otherwise create a output matrix and fill the cells...\n        output = [[0 for i in range(c)] for i in range(r)]\n        idx = 0\n        # Traverse the matrix through the loop... \n        while idx < r * c:\n            # idx % c will give us the current column number...\n            # idx / c will give us how many rows we have completely filled...\n            output[idx // c][ idx % c] =  mat[idx // len(mat[0])][idx % len(mat[0])]\n            idx += 1\n        return output       # Return the output matrix...\n```\n                   \n# **JavaScript Solution:**\n```\n// Time Complexity : O(r*c)\n// Space Complexity : O(r*c)\nvar matrixReshape = function(mat, r, c) {\n    // If transformation doesn\'t occur, return mat...\n    if (mat.length * mat[0].length != r * c) {\n        return mat;\n    }\n    // Otherwise create a output matrix and fill the cells...\n    const output = new Array(r).fill(0).map(() => new Array(c).fill(0));\n    // Traverse the matrix through the loop... \n    for (let idx = 0; idx < r * c; idx++) {\n        // idx % c will give us the current column number...\n        // idx / c will give us how many rows we have completely filled...\n        output[Math.floor(idx/c)][idx % c] = mat[Math.floor(idx / mat[0].length)][idx % mat[0].length];\n    }\n    return output;      // Return the output matrix...\n};\n```\n**I am working hard for you guys...\nPlease upvote if you found any help with this code...**
['Array', 'C', 'Matrix', 'Simulation', 'Python', 'Java', 'Python3', 'JavaScript']