Skip to content

Commit c0eb73a

Browse files
Rotation of an array without using extra space
1 parent 40620ae commit c0eb73a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Others;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Rotation of array without using extra space
7+
*
8+
*
9+
* @author Ujjawal Joshi
10+
* @date 2020.05.18
11+
*/
12+
13+
class main{
14+
public static void main(String[] args)
15+
{
16+
Scanner sc=new Scanner(System.in);
17+
int n=sc.nextInt();
18+
int a[][]=new int[n][n];
19+
20+
for(int i=0;i<n;i++)
21+
{
22+
for(int j=0;j<n;j++)
23+
{
24+
a[i][j]=sc.nextInt();
25+
}
26+
}
27+
int temp=0;
28+
29+
// Rotation of array by swapping their values
30+
for(int i=0;i<n/2;i++)
31+
{
32+
for(int j=i;j<n-i-1;j++)
33+
{
34+
temp=a[i][j];
35+
a[i][j]=a[n-j-1][i];
36+
a[n-j-1][i]=a[n-i-1][n-j-1];
37+
a[n-i-1][n-j-1]=a[j][n-i-1];
38+
a[j][n-i-1]=temp;
39+
}
40+
}
41+
42+
// printing of sorted array
43+
for(int i=0;i<n;i++){
44+
for(int j=0;j<n;j++){
45+
System.out.print(a[i][j]+" ");
46+
47+
}
48+
System.out.println();
49+
}
50+
51+
}
52+
53+
54+
}

0 commit comments

Comments
 (0)