Posted on

Description

Submission

#include <vector>
#include <iostream>

using namespace std;

/*
 * Complete the 'dynamicArray' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER n
 *  2. 2D_INTEGER_ARRAY queries
 */

vector<int> dynamicArray(int n, vector<vector<int>> queries) {
    vector<int> res;
    vector<vector<int>> seqList(n);
    int lastAnswer = 0;

    for(int i = 0; i < queries.size(); ++i) {
        int x = queries[i][1];
        int y = queries[i][2];
        vector<int>& seq = seqList[(lastAnswer ^ x) % n];
        if(queries[i][0] == 1) {
            seq.push_back(y);
        }
        if(queries[i][0] == 2) {
            lastAnswer = seq[y % seq.size()];
            res.push_back(lastAnswer);
        }
    }
    return res;
}


int main()
{
    int n, q; cin >> n >> q;

    vector<vector<int>> queries(q);

    for (int i = 0; i < q; i++) {
        queries[i].resize(3);

        for (int j = 0; j < 3; j++) {
            cin >> queries[i][j];
        }
    }

    vector<int> result = dynamicArray(n, queries);

    for (int i = 0; i < result.size(); i++) {
        cout << result[i];

        if (i != result.size() - 1) {
            cout << "\n";
        }
    }
    cout << "\n";

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *