-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArrayList.java
More file actions
41 lines (41 loc) · 974 Bytes
/
Copy pathRotateArrayList.java
File metadata and controls
41 lines (41 loc) · 974 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
/*
Q7. Given an ArrayList and a value k, rotate the list to the right by k positions:
Example: [1,2,3,4,5], k=2 → [3,4,5,1,2]
What you practice:
Modular arithmetic
Using temporary lists
Index manipulation
*/
import java.util.*;
public class RotateArrayList
{
public static void main(String x[])
{
Scanner sc = new Scanner(System.in);
ArrayList al = new ArrayList();
System.out.print("Enter the total elements: ");
int n = sc.nextInt();
System.out.print("Enter the value: ");
for(int i= 1; i <= n; i++)
{
int num = sc.nextInt();
al.add(num);
}
System.out.println(al);
System.out.print("Enter the value of k: ");
int k = sc.nextInt();
// taking second temporary array list
ArrayList temp = new ArrayList();
//inserting value from k to end
for(int i = k; i < al.size(); i++)
{
temp.add(al.get(i));
}
// inserting 0th to k'th position
for(int i = 0; i < k; i++)
{
temp.add(al.get(i));
}
System.out.print(temp);
}
}