-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotatingArray.java
More file actions
47 lines (31 loc) · 940 Bytes
/
RotatingArray.java
File metadata and controls
47 lines (31 loc) · 940 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* 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 javaapplication2;
/**
*
* @author SAMUEL ADOGA
*/
public class RotatingArray {
public static void main(String[] args){
int[] testarray = {1,2,3,4,5,6};
int[] rotated_array = rotateBy(testarray, 2);
for(int x : rotated_array){
System.out.print(" "+x);
}
}
public static int[] rotateBy(int[] array , int n){
int[] rotated = {};
int lastnum = 0;
for(int i = 0; i < n ; i++){
lastnum = array[array.length-1];
for(int j = array.length-1; j >=1 ; j--){
array[j] = array[j-1];
}
array[0] = lastnum;
}
return array;
}
}