-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotate _array-90_degrees
83 lines (79 loc) · 2.39 KB
/
Rotate _array-90_degrees
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Note: How to make copy of 2-d Array
import java.io.*;
import java.util.*;
public class Solution {
public static void transpose(int[][]arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<=i;j++){
int temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
public static void display(int[][]arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
public static void anticlockwise(int[][] arr1){
///reverse row elements
for(int i=0;i<arr1.length;i++){
int li=0;
int ri=arr1[i].length-1;
while(li<ri){
int temp=arr1[i][li];
arr1[i][li]=arr1[i][ri];
arr1[i][ri]=temp;
li++;ri--;
}
}
transpose(arr1);
}
public static void clockwise(int[][]arr){
transpose(arr);
int k=arr.length-1;
///column reverse
for(int i=0;i<arr.length;i++){
int li=0;
int ri=arr[i].length-1;
while(li<ri){
int temp=arr[i][li];
arr[i][li]=arr[i][ri];
arr[i][ri]=temp;
li++;ri--;
}
}
}
public static int[][] makeCopy(int[][] array){
int[][] b=new int[array.length][];
for(int row=0; row<array.length; ++row){
b[row]=new int[array[row].length];
for(int col=0; col<b[row].length; ++col){
b[row][col]=array[row][col];
}
}
return b;
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[][]arr=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j]=scn.nextInt();
}
}
// display(arr);
int[][] arr1=makeCopy(arr);
clockwise(arr);
display(arr);
anticlockwise(arr1);
display(arr1);
}
}