Skip to content

Commit 089db38

Browse files
solves dynamic array
1 parent ac76b8d commit 089db38

File tree

1 file changed

+60
-59
lines changed

1 file changed

+60
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
1-
import java.util.ArrayList;
2-
import java.util.HashMap;
3-
import java.util.Scanner;
4-
5-
public class DynamicArray {
6-
7-
private static Scanner s = new Scanner(System.in);
8-
private static HashMap<Integer, ArrayList<Integer>> seqList = new HashMap<>();
9-
private static int lastAnswer = 0, N;
10-
11-
public static void main(String[] args) {
12-
int queries;
13-
14-
N = s.nextInt();
15-
queries = s.nextInt();
16-
17-
performQueries(queries);
18-
}
19-
20-
private static void performQueries(int queries) {
21-
int n, x, y;
22-
23-
for(int i=0 ; i<queries ; i++){
24-
n = s.nextInt();
25-
x = s.nextInt();
26-
y = s.nextInt();
27-
28-
if(n == 1)
29-
performQuery1(x, y);
30-
else
31-
performQuery2(x, y);
32-
}
33-
}
34-
35-
private static void performQuery1(int x, int y) {
36-
int index = indexOf(x);
37-
if(seqList.containsKey(index)){
38-
ArrayList<Integer> smallList = seqList.get(index);
39-
smallList.add(y);
40-
seqList.put(index, smallList);
41-
} else {
42-
ArrayList<Integer> smallList = new ArrayList<>();
43-
smallList.add(y);
44-
seqList.put(index, smallList);
45-
}
46-
}
47-
48-
private static void performQuery2(int x, int y){
49-
int index = indexOf(x);
50-
ArrayList<Integer> smallList = seqList.get(index);
51-
int element = smallList.get(y%smallList.size());
52-
lastAnswer = element;
53-
System.out.println(lastAnswer);
54-
}
55-
56-
private static int indexOf(int x){
57-
return (x^lastAnswer)%N ;
58-
}
59-
}
1+
package arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.Scanner;
6+
7+
public class DynamicArray {
8+
9+
private static final Scanner s = new Scanner(System.in);
10+
private static final HashMap<Integer, ArrayList<Integer>> seqList = new HashMap<>();
11+
private static int lastAnswer = 0, N;
12+
13+
public static void main(String[] args) {
14+
int queries;
15+
16+
N = s.nextInt();
17+
queries = s.nextInt();
18+
19+
performQueries(queries);
20+
}
21+
22+
private static void performQueries(int queries) {
23+
int n, x, y;
24+
25+
for(int i=0 ; i<queries ; i++){
26+
n = s.nextInt();
27+
x = s.nextInt();
28+
y = s.nextInt();
29+
30+
if(n == 1)
31+
performQuery1(x, y);
32+
else
33+
performQuery2(x, y);
34+
}
35+
}
36+
37+
private static void performQuery1(int x, int y) {
38+
int index = indexOf(x);
39+
if(seqList.containsKey(index)){
40+
ArrayList<Integer> smallList = seqList.get(index);
41+
smallList.add(y);
42+
seqList.put(index, smallList);
43+
} else {
44+
ArrayList<Integer> smallList = new ArrayList<>();
45+
smallList.add(y);
46+
seqList.put(index, smallList);
47+
}
48+
}
49+
50+
private static void performQuery2(int x, int y){
51+
int index = indexOf(x);
52+
ArrayList<Integer> smallList = seqList.get(index);
53+
lastAnswer = smallList.get(y%smallList.size());
54+
System.out.println(lastAnswer);
55+
}
56+
57+
private static int indexOf(int x){
58+
return (x^lastAnswer)%N ;
59+
}
60+
}

0 commit comments

Comments
 (0)