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.
Scanner in = new Scanner(System.in);
int R = in.nextInt();
int C = in.nextInt();
int times = in.nextInt();
int[][] arr = new int[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
int n = in.nextInt();
arr[i][j] = n;
}
}
int[][] output = null;
while (times > 0) {
// row start
int RS = 0;
// row end
int RE = R - 1;
// column start
int CS = 0;
// column end
int CE = C - 1;
output = new int[R][C];
while (CE > CS) {
for (int i = CS; i < CE; i++) {
output[RS][i] = arr[RS][i + 1];
}
for (int i = RS + 1; i <= RE; i++) {
output[i][RS] = arr[i - 1][RS];
}
for (int i = CS + 1; i <= CE; i++) {
output[RE][i] = arr[RE][i - 1];
}
for (int i = RS; i < RE; i++) {
output[i][CE] = arr[i + 1][CE];
}
RS++;
CS++;
RE--;
CE--;
}
times--;
arr = output;
}
print(output);
}
public static void print(int[][] arr) {
for (int[] a : arr) {
for (int b : a) {
System.out.print(b + " ");
}
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 Rotation
You are viewing a single comment's thread. Return to all comments →
public static void main(String[] args) {