-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDynamicArray.java
More file actions
81 lines (59 loc) · 1.67 KB
/
Copy pathDynamicArray.java
File metadata and controls
81 lines (59 loc) · 1.67 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* 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 hackerrank;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Jayit
*/
public class DynamicArray {
// VERY IMPORTANT CONCEPTS
// So To make a 2D List ...make a 1D list say seqList
// Now each instance of seqList will contain another List say seq
// So seqList is a List of List thus List<List<Integer>>
// And each instance
List<Integer> seq = new ArrayList<Integer>();
List<List<Integer>> seqList = new ArrayList<List<Integer>>();
int lastAns = 0;
public DynamicArray(int N){
for(int i=0;i<=N;i++){
seq = new ArrayList<Integer>();
seqList.add(seq);
}
}
void appendValue(int x,int y,int N){
int rowIndex = (x ^ lastAns) % N;
List<Integer> seq = seqList.get(rowIndex);
seq.add(y);
}
private void printValue(int x, int y,int N){
int colIndex = 0;
int rowIndex = (x^lastAns) % N;
List<Integer> seq = seqList.get(rowIndex);
colIndex = y % seq.size();
lastAns = seq.get(colIndex);
System.out.println(lastAns);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int Q = sc.nextInt();
DynamicArray da = new DynamicArray(N);
for (int i = 0; i < Q; i++) {
int queryType = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if (queryType == 1) {
da.appendValue(x, y, N);
} else {
da.printValue(x, y, N);
}
}
sc.close();
}
}