• + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/MaT-4dnozJI

    Solution 1 O(n + m) :

    vector<int> circularArrayRotation(vector<int> a, int k, vector<int> queries) {
        vector<int> newArray(a.size()), result;
        for(int i = 0; i < a.size(); i++){
            newArray[(i + k) % a.size()] = a[i];
        }
        for(int i = 0; i < queries.size(); i++) result.push_back(newArray[queries[i]]);
        return result;
    }
    

    Solution 2 O(m) :

    vector<int> circularArrayRotation(vector<int> a, int k, vector<int> queries) {
        vector<int> result;
        for(int i = 0; i < queries.size(); i++) {
            int previousIndex = (queries[i] - k + a.size() * 100000 ) % a.size();
            result.push_back(a[previousIndex]);
        }
        return result;
    }