# Dynamic Array HackerRank Solution Dynamic Array HackerRank Solution

In this Dynamic Array HackerRank solution, Declare a 2-dimensional array, , of  empty arrays. All arrays are zero indexed.

• Declare an integer, , and initialize it to .
• There are  types of queries, given as an array of strings for you to parse:
1. Query: 1 x y
1. Let .
2. Append the integer  to .
2. Query: 2 x y
1. Let .
2. Assign the value  to .
3. Store the new value of  to an answers array.

Note:  is the bitwise XOR operation, which corresponds to the ^ operator in most languages. Learn more about it on Wikipedia.  is the modulo operator.
Finally, size(arr[idx]) is the number of elements in arr[idx]

Function Description

Complete the dynamicArray function below.

dynamicArray has the following parameters:
int n: the number of empty arrays to initialize in
string queries[q]: query strings that contain 3 space-separated integers

Returns

• int[]: the results of each type 2 query in the order they are presented

Input Format

The first line contains two space-separated integers, , the size of  to create, and , the number of queries, respectively.
Each of the  subsequent lines contains a query string, .

Constraints

• It is guaranteed that query type  will never query an empty array or index.

Sample Input

2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1


Sample Output

7
3


Explanation

Initial Values:

= [ ]
= [ ]

Query 0: Append  to .

= 
= [ ]

Query 1: Append  to .
= 
= 

Query 2: Append  to .

= [5, 3]
= 

Query 3: Assign the value at index  of  to , print .

= [5, 3]
= 

7


Query 4: Assign the value at index  of  to , print .

= [5, 3]
= 

3

## Dynamic Array HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

### Problem Solution in Python

def dynamicArray(n, queries):
arr = [[] for _ in range(n)]
result = []

for t, x, y in queries:
idx = (x ^ lastAnswer) % n
if t == 1:
arr[idx].append(y)
else:

return result

### Problem Solution in C++

vector<int> dynamicArray(int n, vector<vector<int>> queries) {
vector<vector<int>> arr(n);

for (const auto& query : queries)
{
auto idx = (query ^ last_answer.back()) % n;
if (query == 1)
{
arr[idx].push_back(query);
}
if (query == 2)
{
}
}

}

### Problem Solution in JavaScript

function dynamicArray(n, queries) {
let arr = Array.from(Array(n), () => new Array())

queries.forEach(([q,x,y]) => {
let idx = (x ^ lastAnswer) % n;

if(q === 1){
arr[idx].push(y);
} else{
}