We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 5
  5. Dynamic Array

Dynamic Array

Problem
Submissions
Leaderboard
Discussions
Editorial
HackerRank Logo
|
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 5
  5. Dynamic Array
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial
  • 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website