|
1 | 1 | package Others;
|
2 | 2 |
|
3 | 3 | /**
|
4 | | - * Given a matrix of size n x n |
5 | | - * We have to rotate this matrix by 90 Degree |
6 | | - * Here is the algorithm for this problem . |
7 | | - * |
| 4 | + * Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here is the algorithm for |
| 5 | + * this problem . |
8 | 6 | */
|
9 | | - |
10 | 7 | import java.util.*;
|
11 | 8 |
|
12 | 9 | class Rotate_by_90_degree {
|
13 | | - public static void main(String[] args) { |
14 | | - Scanner sc = new Scanner(System.in); |
15 | | - int t = sc.nextInt(); |
16 | | - |
17 | | - while (t-- > 0) { |
18 | | - int n = sc.nextInt(); |
19 | | - int[][] arr = new int[n][n]; |
| 10 | + public static void main(String[] args) { |
| 11 | + Scanner sc = new Scanner(System.in); |
| 12 | + int t = sc.nextInt(); |
20 | 13 |
|
21 | | - for (inti = 0; i < n; i++) |
22 | | - for (int j = 0; j < n; j++) |
23 | | - arr[i][j] = sc.nextInt(); |
| 14 | + while (t-- > 0) { |
| 15 | + int n = sc.nextInt(); |
| 16 | + int[][] arr = newint[n][n]; |
24 | 17 |
|
25 | | - Rotate g = new Rotate(); |
26 | | - g.rotate(arr); |
27 | | - printMatrix(arr); |
| 18 | + for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) arr[i][j] = sc.nextInt(); |
28 | 19 |
|
29 | | - } |
30 | | - sc.close(); |
| 20 | + Rotate g = new Rotate(); |
| 21 | + g.rotate(arr); |
| 22 | + printMatrix(arr); |
31 | 23 | }
|
| 24 | + sc.close(); |
| 25 | + } |
32 | 26 |
|
33 | | - static void printMatrix(int arr[][]) { |
34 | | - for (int i = 0; i < arr.length; i++) { |
35 | | - for (int j = 0; j < arr[0].length; j++) |
36 | | - System.out.print(arr[i][j] + " "); |
37 | | - System.out.println(""); |
38 | | - } |
| 27 | + static void printMatrix(int arr[][]) { |
| 28 | + for (int i = 0; i < arr.length; i++) { |
| 29 | + for (int j = 0; j < arr[0].length; j++) System.out.print(arr[i][j] + " "); |
| 30 | + System.out.println(""); |
39 | 31 | }
|
| 32 | + } |
40 | 33 | }
|
41 | 34 |
|
42 | | -/** |
43 | | - * Class containing the algo to roate matrix by 90 degree |
44 | | - */ |
45 | | - |
| 35 | +/** Class containing the algo to roate matrix by 90 degree */ |
46 | 36 | class Rotate {
|
47 | | - static void rotate(int a[][]) { |
48 | | - int n = a.length; |
49 | | - for (int i = 0; i < n; i++) { |
50 | | - for (int j = 0; j < n; j++) { |
51 | | - if (i > j) { |
52 | | - int temp = a[i][j]; |
53 | | - a[i][j] = a[j][i]; |
54 | | - a[j][i] = temp; |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - int i = 0, k = n - 1; |
59 | | - while (i < k) { |
60 | | - for (int j = 0; j < n; j++) { |
61 | | - int temp = a[i][j]; |
62 | | - a[i][j] = a[k][j]; |
63 | | - a[k][j] = temp; |
64 | | - } |
65 | | - |
66 | | - i++; |
67 | | - k--; |
| 37 | + static void rotate(int a[][]) { |
| 38 | + int n = a.length; |
| 39 | + for (int i = 0; i < n; i++) { |
| 40 | + for (int j = 0; j < n; j++) { |
| 41 | + if (i > j) { |
| 42 | + int temp = a[i][j]; |
| 43 | + a[i][j] = a[j][i]; |
| 44 | + a[j][i] = temp; |
68 | 45 | }
|
69 | | - |
| 46 | + } |
| 47 | + } |
| 48 | + int i = 0, k = n - 1; |
| 49 | + while (i < k) { |
| 50 | + for (int j = 0; j < n; j++) { |
| 51 | + int temp = a[i][j]; |
| 52 | + a[i][j] = a[k][j]; |
| 53 | + a[k][j] = temp; |
| 54 | + } |
| 55 | + |
| 56 | + i++; |
| 57 | + k--; |
70 | 58 | }
|
| 59 | + } |
71 | 60 | }
|
0 commit comments