Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Python code Declare a 2 - dimensional array, arr, of n empty arrays. All arrays are zero indexed. Declare an integer, lastAnswer, and initialize it

Python code
Declare a 2-dimensional array, arr, of n empty arrays. All arrays are zero indexed.
Declare an integer, lastAnswer, and initialize it to 0.
There are 2 types of queries, given as an array of strings for you to parse:
1. Query: 1 x y
1. Let idx=((xlastAnswer)%n).
2. Append the integer y to arr[idx].
2. Query: 2 x y
1. Let idx=((xlastAnswer)%n).
2. Assign the value arr[idx][y%size([arridx])] to lastAnswer.
3. Store the new value of lastAnswer 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 arr
- 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, n, the size of arr to create, and q, the number of queries, respectively.
Each of the q subsequent lines contains a query string, queries[i].
Constraints
1<=n, q<=10^5
0<=x, y<=10^9
It is guaranteed that query type 2 will never query an empty array or index.
Sample Input
25
105
117
103
210
211
Sample Output
7
3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students explore these related Databases questions