Dynamic Array

  • + 0 comments
    def dynamicArray(n, queries):
        lastAnswer = 0
        arr = []
        answers = []
        for i in range(n):
            arr.append([])
        for query in queries:
            idx = (query[1]^ lastAnswer) % n
            if query[0] == 1:
                arr[idx].append(query[2])
            if query[0] == 2:
                lastAnswer = arr[idx][(query[2])% (len(arr[idx]))]
                answers.append(lastAnswer)
        return (answers)