Dynamic Array

  • + 0 comments

    Python 3

    def dynamicArray(n, queries):
        arr = [[]for _ in range(n)]
        lastAnswer = 0
        result = []
        for querie in queries:
            x = querie[1]
            y = querie[2]
            
            idx = ((x ^ lastAnswer)% n)
    				
            if (querie[0]==1):
                arr[idx].append(y)
            
            if (querie[0]==2):
                lastAnswer = arr[idx][y % len(arr[idx])]
                result.append(lastAnswer)
        return result