public int[][] matrixReshape(int[][] nums, int r, int c) {

int newNums[] = null;

int mp[][]=null;

int len=0;

int index=0;

for(int i=0;i<nums.length;i++){

len+=nums[i].length;

}

newNums=new int [len];

for(int i=0;i<nums.length;i++){

for(int j=0;j<nums[i].length;j++){

newNums[index++]=nums[i][j];

}

}

int z=0;

int m=rc;

int firstLength=nums.length;

int secondLength=nums[0].length;

if(firstLengthsecondLength==m){

mp=new int[r][c];

for(int i=0;i<r;i++){

for(int j=0;j<c;j++){

mp[i][j]=newNums[z++];

}

}

}

//my runtime beats 43.55 % of java submissions