Sort by

recency

|

3074 Discussions

|

  • + 0 comments

    My solution

    def _rotate(a, k): return a[-k:]+a[0:k]

    def circularArrayRotation(a, k, queries): # Write your code here res=_rotate(a, k) return [res[i] for i in queries]

  • + 0 comments
    k = int(first_multiple_input[1])
            ~~~~~~~~~~~~~~~~~~~~^^^
    

    IndexError: list index out of range

  • + 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;
    }
    
  • + 1 comment
    `function circularArrayRotation(a, k, queries) {
        let n = a.length; 
        k = k % n; 
        
        let res = []; 
        
        for(let q of queries) {
            res.push( a[ (q - k + n) % n]); 
        }
        
        return res;
    }
    

    `

  • + 0 comments

    def reverse(a,l,r): i=l j=r while i

    def circularArrayRotation(a, k, queries): k=k%len(a) reverse(a,0,len(a)-1) reverse(a,0,k-1) reverse(a,k,len(a)-1) l=[] for i in queries: l.append(a[i]) return l