HackerRank Dynamic Array problem solution

In this HackerRank Dynamic Array problem, we need to develop a program in which we need to perform the queries using the bitwise operations.

Problem solution in Python programming.

```a = input().split(' ')
N, Q = [int(e) for e  in a]
arrays = [[] for _ in range(N)]
lastans = 0

def insert(x, y):
global lastans
arrays[(x ^ lastans) % N].append(y)
def printnum(x, y):
global lastans
inst = arrays[(x ^ lastans) % N]
lastans = inst[y % len(inst)]
print(lastans)

for i in range(Q):
line = [int(e) for e in input().split(' ')]
insert(line[1], line[2]) if line[0] == 1 else printnum(line[1], line[2])```

Problem solution in Java Programming.

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>(n);
for (int i = 0; i < n; i++) {
}
int q = sc.nextInt();
int lastans = 0;
for (int i = 0; i < q; i++) {
if (sc.nextInt() == 1) {
}
else {
ArrayList<Integer> l = lists.get((sc.nextInt() ^ lastans) % n);
lastans = l.get(sc.nextInt() % l.size());
System.out.println(lastans);
}
}
}
}```

Problem solution in C++ programming.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int N = 0, Q = 0;
int lastans = 0;
int type, x, y;
int seq = 0;
int pos;

cin >> N >> Q;

vector < vector <int>> a(N);

for (int i = 0; i < Q; i ++) {
cin >> type >> x >> y;
seq = ((x ^ lastans) % N);
// cout << seq << endl;
if (type == 1) {
a[seq].push_back(y);
// cout << (seq) << " <-- " << y << endl;
}
else if (type == 2) {
pos = (y % a[seq].size());
//cout << "pos " << pos << endl;
lastans = a[seq][pos];
cout << lastans << endl;
}

}
return 0;
}```

Problem solution in C programming.

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Seq_ {
int *dat;
int sz;
} Seq;

Seq seq[100001];

void insert(int n, int y) {
seq[n].dat = realloc(seq[n].dat, seq[n].sz += 1);
seq[n].dat[seq[n].sz-1] = y;
}

int print(int n, int y) {
int t = seq[n].dat[y % seq[n].sz];
printf("%d\n", t);
return t;
}

int main() {
FILE *f = stdin;

int n, q;
fscanf(f, "%d %d", &n, &q);
int lastans = 0;
for (int i = 0; i < q; i++) {
int op, x, y;
fscanf(f, "%d %d %d", &op, &x, &y);
int seq = (x ^ lastans) % n;
if (op == 1)
insert(seq, y);
else // op == 2
lastans = print(seq, y);
}

return 0;
}```

Problem solution in JavaScript programming.

```function processData(input) {
var lines = input.split('\n');
var NQ = lines[0].split(' ').map(Number);
var N = NQ[0];
var Q = NQ[1];
var lastans = 0;
var sequences = [];
for (var i = 0; i <= N; i++) {
sequences[i] = [];
}
lines.slice(1).forEach(function (line) {
var query = line.split(' ').map(Number);
var x = query[1];
var y = query[2];
if (query[0] === 1) {
var sequenceNum = (x ^ lastans) % N;
sequences[sequenceNum].push(y);
} else if (query[0] === 2) {
var sequenceNum = (x ^ lastans) % N;
var size = sequences[sequenceNum].length;
var element = sequences[sequenceNum][y % size];
console.log(element);
lastans = element;
}
});
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```