We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // number of sequences
int q = in.nextInt(); // number of queries
// initialize n empty sequences
ArrayList<ArrayList<Integer>> seqList = new ArrayList<ArrayList<Integer>>(n);
for (int i = 0; i < n; i++) {
seqList.add(new ArrayList<Integer>());
}
int lastAnswer = 0;
// process queries
for (int i = 0; i < q; i++) {
int type = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int idx = (x ^ lastAnswer) % n; // XOR and mod operation
if (type == 1) {
// append y to sequence at index idx
seqList.get(idx).add(y);
} else if (type == 2) {
ArrayList<Integer> seq = seqList.get(idx);
lastAnswer = seq.get(y % seq.size());
System.out.println(lastAnswer);
}
}
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dynamic Array
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.io.; import java.util.*;
public class Solution {
}