[HackerRank] Dynamic Array

Dynamic Array

Problem

https://www.hackerrank.com/challenges/dynamic-array/problem

Solution

vector<int> dynamicArray(int n, vector<vector<int>> queries) {
    vector<int> answers;
    int lastAnswer = 0;
    vector<vector<int>> seqs(n,vector<int>());

    for (size_t i = 0; i < queries.size(); i++) {
        int query_type = queries[i][0];
        int x = queries[i][1];
        int y = queries[i][2];
        int seqIndex = (x^lastAnswer)%n;
        if(query_type == 1){
            seqs[seqIndex].push_back(y);
        }else if (query_type == 2) {
            lastAnswer = seqs[seqIndex][y % seqs[seqIndex].size()];
            answers.push_back(lastAnswer);
        }

    }
    return answers;
}

Leave a Reply

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