Problem statement:
Rotate the matrix 90 degree in anti clock wise
Input:
1 2 3
4 5 6
7 8 9
Output:
3 6 9
2 5 8
1 4 7
Solution:
public void matrixRotation() {
int[][] array = { { 1, 2, 3}, { 4, 5, 6},{7,8,9} };
int rowCount = array.length;
int colCount = array[0].length;
for (int i = colCount - 1; i >= 0; i--) {
for (int j = 0; j < rowCount; j++) {
System.out.print(array[j][i] + " ");
}
System.out.println();
}
}
No comments:
Post a Comment