Sort by

recency

|

3077 Discussions

|

  • + 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))
    
  • + 0 comments

    JS/Javascript solutiuon:-

    function circularArrayRotation(a, k, queries) {
      const actualRotation = k % a.length;
      let newArr = a;
      if (actualRotation) {
        const rightPart = a.splice(a.length - actualRotation);
        newArr = [...rightPart, ...a];
      }
      return queries.map((query) => newArr[query]);
    }
    
  • + 0 comments

    C++ Solution

    vector circularArrayRotation(vector a, int k, vector queries) { vector res; for(auto& q : queries) { int index = (q + a.size() - k%a.size()); if(index >= a.size()) index = index % a.size(); res.push_back(a[index]); } return res; }

  • + 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]