-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateImage.js
More file actions
29 lines (25 loc) · 741 Bytes
/
rotateImage.js
File metadata and controls
29 lines (25 loc) · 741 Bytes
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
const rotateMatrix = () => {
let arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]];
// step 1- Find the transpose
for(let i = 0; i < arr.length; i++){
for(let j = i+1; j < arr[i].length; j++){
const temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
// find the reverse of the transposed matrix
for(let i = 0; i<arr.length; i++){
let start = 0;
let end = arr.length-1;
while(start < end){
const temp = arr[i][start];
arr[i][start] = arr[i][end];
arr[i][end] = temp;
start++;
end--;
}
}
console.log("Rotated matrix is: ", arr);
}
rotateMatrix();