Dynamic Array

  • + 0 comments

    Javascript

    function dynamicArray(n: number, queries: number[][]): number[] {
      const arr = new Array(n);
      let lastAnswer = 0;
      const answers: number[] = [];
      
      queries.forEach(([qType, x, y]) => {
        const idx = (x ^ lastAnswer) % n;
        if (!arr[idx]) {
          arr[idx] = [];
        }
        
        if (qType === 1) {
          arr[idx].push(y);
        } else if (qType === 2) {
          lastAnswer = arr[idx][y % arrSize(arr[idx])];
          answers.push(lastAnswer);
        }
      });
      
      return answers;
    }