Sort by

recency

|

3080 Discussions

|

  • + 0 comments

    Here is my Python solution! We insert the last item at the front and then remove the last item however many times we need to, and then return where each value that we need to return.

    def circularArrayRotation(a, k, queries):
        for i in range(k):
            a.insert(0, a[-1])
            a.pop(-1)
        return [a[i] for i in queries]
    
  • + 0 comments

    JavaScript solution in 0(n) time 0(1) space without copying or modifying the given array.

    function circularArrayRotation(a, k, queries) { return queries.map(q => a.at((q - k) % a.length)) }

  • + 0 comments

    for(int i=0;i

  • + 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;
    }
    
  • + 0 comments
    def circularArrayRotation(a, k, queries):
        return list(map(lambda x: a[x - (k % len(a))], queries))