✅Simple || Java || Beats 100% runtime || Easy to understand.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to arrange the elements of the matrix into new matrix of given row and column.\
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to arrange the elements of the matrix into new matrix of given row and column.\nWe have to check whether it is possible to construct a new array or not.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n    1. Check the base conditions.\n    2. Create a new array.\n    3. Traverse and store the elements.\nFollow the code below to understand the solution.\n\n             **If this solution helped you, give it an up-vote to help others** \n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n\n        int oldRow = mat.length;\n        int oldCol = mat[0].length;\n\n\n        //We have to check whether it is possible to construct-\n        //-a new array or not.\n        //we can check it by comparing the total elements.\n        if(oldCol*oldRow != r*c) return mat;\n\n        int [] [] arr = new int[r][c];\n        int i = 0;      // row of new array\n        int j = 0;      // col of new array\n        int k = 0;      // row of old array\n        int l = 0;      // col of old array\n\n        //Traverse the new array and put elements from the old array.\n        for(i = 0; i < r; i++)\n        {\n            for(j = 0; j < c; j++)\n            {\n                arr[i][j] = mat[k][l];\n\n                //We also need to update the indices of the old array.\n                //If column length is reached then increment row\n                // and reset column to 0;\n                l++;\n                if(l == oldCol)\n                {\n                    k++;\n                    l = 0;\n                }\n\n            }\n        }\n        //Finally, return the new array;\n        return arr;\n        \n    }\n}\n```
['Java']