-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRotateArray.java
38 lines (33 loc) · 978 Bytes
/
RotateArray.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
package Problems_on_Arrays;
import java.util.Scanner;
public class RotateArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of elements:");
int n=sc.nextInt();
int[] arr=new int[n];
for (int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("enter the value of K:");
int k=sc.nextInt();
//simplify k by using modulo operator with the length of the array
k=k%n;
//make a duplicate Array
int[] res=new int[n];
int counter=0;
for (int i=k;i<n;i++){
res[i]=arr[counter];
counter++;
}
for (int i=0;i<k;i++){
res[i]=arr[counter];
counter++;
}
//displaying resultant array
for (int i=0;i<n;i++){
System.out.print(res[i]+" ");
}
sc.close();
}
}