Wednesday, 4 May 2022

Matrix rotation

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

Switch case in Java

 Problem statement: Return the capital of a state based on input state          public String getCapital(String state){               switch...