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