-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotate90.java
69 lines (59 loc) · 1.69 KB
/
Rotate90.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package learning;
/**
*
* @author akshatsharma
*/
public class Rotate90 {
int n;
int oArray[][];
public Rotate90(int n) {
this.n = n;
this.oArray = new int[n][n];
initialiseArray();
}
void initialiseArray() {
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
oArray[i][j] = (int)(Math.random() * 49 + 1);
}
}
}
void printArray(int arr[][]) {
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr.length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
void roateArray(int arr[][]){
int singleDArray[] = new int[n * n];
int count = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
singleDArray[count++] = arr[i][j];
}
}
int singleDRotatedArray[] = new int[n * n];
int rCount = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
singleDRotatedArray[rCount++] = singleDArray[(j*n)+n-i-1];
}
}
int rotatedArr[][] = new int[n][n];
int c = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
rotatedArr[i][j] = singleDRotatedArray[c++];
}
}
System.out.println("-----ROTATED ARRAY-----");
printArray(rotatedArr);
}
}