Sort by

recency

|

3106 Discussions

|

  • + 0 comments

    My python solution:

    def swap(arr):
        el = arr.pop(-1)
        arr.insert(0, el)
        return arr
    
    def circularArrayRotation(a, k, queries):
        for i in range(k%len(a)):
            a = swap(a)
            
        return [a[x] for x in queries]
    
  • + 0 comments

    My C++ solution

    vector circularArrayRotation(vector & arr, int k, vector queries) { vector res; // k % arr.size() enusre that we are not unnecessarily rotating back to original std::rotate(arr.rbegin(), arr.rbegin() + (k % arr.size()), arr.rend());

    for (auto a : queries)
        res.push_back(arr[a]);
    
    return res;
    

    }

  • + 0 comments

    function circularArrayRotation(a, k, queries) {

        let x = k % a.length;
        let result = [];
        for(let i = 0; i < queries.length; i++) {
            // the formal is (queries[i] + a.length - x) % a.length to get the right index after rotation 
            result.push(a[(queries[i] + a.length - x) % a.length]);
        }
        return result;
    

    }

  • + 0 comments

    in scala without actually rotating

    def circularArrayRotation(a: Array[Int], k: Int, queries: Array[Int]): Array[Int] = {
      val kMod = k % a.length
      queries.map(x => a(if ((x - kMod + a.length) > 0) (x - kMod + a.length) % a.length else 0))
    }
    
  • + 0 comments
    def circularArrayRotation(a:list, k:int, queries:list):
        ### Shift Circulate 
        result = []
        for x in range(k):
            last = a[-1]
            a.insert(0,last)
            a.pop(-1)
        
        ### add numbers to result based on queary
        for num in queries:
            result.append( a[num])
        return result