Sort by

recency

|

3109 Discussions

|

  • + 0 comments

    List res=new ArrayList<>(); int n=a.size(); k=k%n; for(int q:queries){ int newIndex=(q-k+n)%n; res.add(a.get(newIndex)); } return res;

  • + 0 comments

    Test failed succesfully???

    Test case 4 says success but it's marked red, as if it failed?

  • + 0 comments
    def circularArrayRotation(a, k, queries):
        # Example: For array [1, 2, 3, 4, 5] (n=5):
        # k=0: [1, 2, 3, 4, 5] (no rotation)
        # k=1: [5, 1, 2, 3, 4] (rotated right once)
        # k=2: [4, 5, 1, 2, 3] (rotated right twice)
        # ...
        # k=5: [1, 2, 3, 4, 5] (full rotation, back to original)
        
        # To rotate efficiently without multiple single shifts:
        # 1. Calculate the effective rotation offset using modulo arithmetic
        #    (since rotating by n positions brings array back to original)
        # 2. Split the array at the offset and swap the two parts
        
        # Calculate the rotation point
        offset = len(a) - k % len(a)
        
        # Perform the rotation by concatenating the two slices
        rotated = a[offset:] + a[:offset]
        
        # Return the elements at the requested indices
        return [rotated[q] for q in queries]
    
  • + 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;
    

    }