-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic Array.cpp
31 lines (24 loc) · 950 Bytes
/
Dynamic Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Problem Title: Dynamic Array
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-dynamic-array/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=preparation-kits&playlist_slugs%5B%5D%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D%5B%5D=one-month-week-two
Max Score: 100
Score: 100
Language: C++
Category: One Month Preparation
*/
vector<int> dynamicArray(int n, vector<vector<int>> queries) {
vector<int>answers; int lastAnswer = 0; vector<vector<int>> arr;
for (int i = 0; i < n; ++i) arr.push_back(vector<int>());
for (int i = 0; i < queries.size(); ++i)
{
int q = queries[i][0], x = queries[i][1], y = queries[i][2];
if (q == 1)
arr[(x^lastAnswer)%n].push_back(y);
else {
int idx = (x^lastAnswer)%n;
lastAnswer = arr[idx][y%arr[idx].size()];
answers.push_back(lastAnswer);
}
}
return answers;
}