Best Solution in JAVA beats 100% JAVA solutions in RUN TIME ✅✅
Intuition\n Describe your first thoughts on how to solve this problem. \nCheck if the matrix can fit in new matrix or not and copy elements to the new one. \n# 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck if the matrix can fit in new matrix or not and copy elements to the new one. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Check if the matrix can fit in new matrix if true copy elements to the new one. Else return old one.\n2. Make two new variable x,y to be used as index of new matrix.\n3. Run nested for loop for coping data from the old matrix and paste it in new matrix using the variables x,y.\n4. For indexing of new variable use  \n5.              y++;\n                if(y == c){\n                    y = 0;\n                    x++;\n                }\nIf you Understand the Solution\'s Approch do Upvote \uD83D\uDE4F\uD83C\uDFFB\uD83D\uDE4F\uD83C\uDFFB\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(m * n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(r * c)\n# Code\n```\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n        int m = mat.length;\n        int n = mat[0].length;\n\n        if(m*n != r*c){\n            return mat;\n        } \n        \n        \n\n        int x = 0;\n        int y = 0;\n        int res[][] = new int[r][c];\n        for(int i = 0; i < m; i++){\n            for(int j = 0; j < n; j++){\n                res[x][y] = mat[i][j];\n                y++;\n                if(y == c){\n                    y = 0;\n                    x++;\n                }\n            }\n        }\n        return res;\n    }\n}\n```
['Java']