Skip to content

Commit 0e6d31b

Browse files
committed
Rotate an array
1 parent 1a278e1 commit 0e6d31b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

hacktober-1/src/rotateAnArray.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
public class rotateAnArray {
3+
4+
public static void main(String[] args) {
5+
// TODO Auto-generated method stub
6+
int[] arr= {10,20,30,40};
7+
rotate(arr,2);
8+
}
9+
public static void rotate(int[] a,int n) {
10+
11+
for(int i=0;i<n;i++) {
12+
int temp=a[a.length-1];
13+
for(int j=a.length-1;j>=1;j--) {
14+
15+
a[j]=a[j-1];}
16+
17+
a[0]=temp;
18+
19+
20+
}
21+
for(int val:a) {
22+
System.out.print(val+" ");
23+
}
24+
25+
}
26+
}

0 commit comments

Comments
 (0)