We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
finally cracked it! thanks for all the suggestions
Here is my java code
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){intr,c,n;Scanners=newScanner(System.in);r=s.nextInt();c=s.nextInt();n=s.nextInt();int[][]mat=newint[r][c];int[][]res=newint[r][c];for(inti=0;i<r;i++)for(intj=0;j<c;j++)mat[i][j]=s.nextInt();for(inti=0;i<r;i++)for(intj=0;j<c;j++){introw=i,col=j;intlayer=Math.min(Math.min(i,j),Math.min(r-i-1,c-j-1));intparam=(r+c-2)*2-8*layer;//no of elements in arrayif(param==0){res[i][j]=mat[i][j];return;}introt=n%param;while(rot-->0){if(row==layer&&col==layer)row++;elseif(row==layer&&col==c-layer-1)col--;elseif(row==r-layer-1&&col==layer)col++;elseif(row==r-layer-1&&col==c-layer-1)row--;elseif(row==layer)col--;elseif(col==layer)row++;elseif(row==r-layer-1)col++;elseif(col==c-layer-1)row--;}// System.out.println(row+" "+col+" by "+i+" "+j);res[row][col]=mat[i][j];}for(inti=0;i<r;i++){for(intj=0;j<c;j++)System.out.print(res[i][j]+" ");System.out.println();}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
finally cracked it! thanks for all the suggestions Here is my java code