forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.java
More file actions
42 lines (38 loc) · 1.51 KB
/
DynamicArray.java
File metadata and controls
42 lines (38 loc) · 1.51 KB
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
import java.io.*;
import java.util.*;
public class DynamicArray {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1[] = br.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int q = Integer.parseInt(s1[1]);
// arraylist of arraylist
ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>(n);
// initialise that there are a total of n lists..
for (int i = 0; i < n; i++) {
ArrayList<Integer> sub = new ArrayList<Integer>();
al.add(sub);
}
int lastans = 0;
while (q-- > 0) {
String s[] = br.readLine().split(" ");
int num = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
int y = Integer.parseInt(s[2]);
if (num == 1) {
int temp = (x ^ lastans) % n;
// getting sub arraylist with index equals to temp and then adding the value of
// y in it.
al.get(temp).add(y);
} else {
int temp = (x ^ lastans) % n;
// finsing size of sub arraylist
int a1 = al.get(temp).size();
// finding integer stored at index in the subarraylist of index equals to temp
lastans = al.get(temp).get(y % a1);
// print statement
System.out.println(lastans);
}
}
}
}