JAVA SOLUTION || DETAILED EXPLANATION || EASY APPROCH || 100% Efficent
Approch -``\n\n1st condition to be checked -\nFirstly will have to check if the product of dimension of the given array matrix(mat) and the product of dimension
# Approch -``\n\n***1st condition** to be checked -*\nFirstly will have to check if the product of dimension of the given array matrix(**mat**) and the product of dimensions of the new array matrix are eqaul. If they are not equal this means we cannot fill all the elements perfectly in one of the matrix hence in this condition, will have to return the original array.\n\n***Secondly***, we will traverse through the first matrix and add the elements in our newly created **output matrix**, but here we have to keep in mind to add elements column wise and not row wise. i.e we will maintain two seperate pointers for rows and colums and firstly we will go through colums & if (**column == c**) then we will move to next row & also set the column pointer to zero again.\n\n***For Better unnderstnading let\'s look at the code***\n\n```\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n        \n        //Storing the values of mat matrix\n        //i.e m = rows & n = cols\n        int rows = mat.length;\n        int cols = mat[0].length;\n        \n        //if the product of rows & cols of mat matrix and the new matrix are not same then return original matrix\n        if((rows * cols) != (r * c)) return mat;\n        \n        //Creating the new matrix\n        int[][] output = new int[r][c];\n        int output_rows = 0;\n        int output_cols = 0;\n        \n        \n        //Traversing the mat matrix and storing the its values in new matrix output cols wise\n        for(int i = 0; i < rows; i++)\n        {\n            for(int j = 0; j < cols; j++)\n            {\n                output[output_rows][output_cols] = mat[i][j];\n                output_cols++;\n                \n                //if the cols value reached then change the row and set the cols value to 0.\n                if(output_cols == c)\n                {\n                    output_cols = 0;\n                    output_rows++;\n                }\n            }\n        }\n        \n        return output;\n    }\n}\n```\n\n\n**If found helpful, please upvote.\nThank You !!**
['Java']