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 .

 = [5]
 = [ ]

Query 1: Append  to .
 = [5]
 = [7]

Query 2: Append  to .

 = [5, 3]
 = [7]

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

 = [5, 3]
 = [7]

7

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

 = [5, 3]
 = [7]

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)]
    lastAnswer = 0
    result = []
   
    for t, x, y in queries:
        idx = (x ^ lastAnswer) % n
        if t == 1:
            arr[idx].append(y)
        else:
            lastAnswer = arr[idx][y % len(arr[idx])]
            result.append(lastAnswer)
   
    return result

Problem Solution in C++

vector<int> dynamicArray(int n, vector<vector<int>> queries) {
    vector<vector<int>> arr(n);
    vector<int> last_answer;
    last_answer.push_back(0);
   
    for (const auto& query : queries)
    {
        auto idx = (query[1] ^ last_answer.back()) % n;
        if (query[0] == 1)
        {
            arr[idx].push_back(query[2]);
        }
        if (query[0] == 2)
        {
            last_answer.push_back(arr[idx][query[2] % arr[idx].size()]);
        }
    }
   
    last_answer.erase(last_answer.begin());
    return last_answer;
}

Problem Solution in JavaScript

function dynamicArray(n, queries) {
    let arr = Array.from(Array(n), () => new Array())
    let lastAnswer = 0;
    let answers = [];
   
    queries.forEach(([q,x,y]) => {
        let idx = (x ^ lastAnswer) % n;
       
        if(q === 1){
            arr[idx].push(y);
        } else{
             lastAnswer = arr[idx][y % arr[idx].length];
             answers.push(lastAnswer);
        }
   
    })
   
    return answers;


}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment