When instancing generic collections, it' best to use the diamond operator to let the compiler figure out the generic arguments. This lets you define in a single place (type definition, not constructor) for easier future modification.
sequenceList[index] = new LinkedList<>();
When using an Object, always declare its type as the highest class in its type hierarchy (up to interface if possible).
You're not using LinkedList's specific methods, so:
List<Integer>[] sequenceList = new List[n];
Javadoc!
Use proper Objects:
- Make Solution a real instance created one in
main (no real job should be performed in main). Then you can make lastAns and n fields of Solution because in the problem statement it is made to be a state of the Solution, so you don't need to pass those around every time
- You could Wrap your
Lists in a Sequence internal class of Solution. Internal means it will be able to access the lastAns field of the Solution instance etc.
It's a bit weird to lazily initialize the sequences. You don't win much, but you put an ugly if in your code. If there is no risk having a huge but sparse array, I would drop this.
Don't one-line ifs, and always use brackets
I'm usually against calling variables x, y etc. On this case it's defined in the problem so it's ok.
Updated code:
public class Solution {
private int n;
private int q;
private int lastAns = 0;
public Solution(int size, int numberOfQuestions) {
n = size;
q = numberOfQuestions;
}
public int getIndex(int x) {
return (x ^ lastAns) % n;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Solution solution = new Solution(in.nextInt(), in.nextInt());
solution.solve(in);
}
public void solve(Scanner in) {
List<Integer>[] sequenceList = new List[n];
while(q-- != 0) {
int queryType = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int index = getIndex(x);
if(queryType == 1) {
if(sequenceList[index] == null) {
sequenceList[index] = new LinkedList<Integer>();
}
sequenceList[index].add(y);
} else if(queryType == 2) {
lastAns = sequenceList[index].get(y % sequenceList[index].size());
System.out.println(lastAns);
}
}
}
}