forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrix.java
49 lines (41 loc) · 848 Bytes
/
RotateMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.*;
class Rotate
{
static int N = 4;
static void rotate90Clockwise(int a[][])
{
// Traverse each cycle
for (int i = 0; i < N / 2; i++)
{
for (int j = i; j < N - i - 1; j++)
{
int temp = a[i][j];
a[i][j] = a[N - 1 - j][i];
a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];
a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];
a[j][N - 1 - i] = temp;
}
}
}
// Function for print matrix
static void printMatrix(int arr[][])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.print( arr[i][j] + " ");
System.out.println();
}
}
// Driver code
public static void main (String[] args)
{
int arr[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
rotate90Clockwise(arr);
printMatrix(arr);
}
}
// This code has been contributed by inder_verma.