Arrays: Left Rotation

  • + 0 comments

    You have O(n) in:

    a = deque(a)
    

    In order to avoid this, you should use a deque from the beginning like:

    from collections import deque
    
    def array_left_rotation(a, n, k):
        a.rotate(-k)
    
    n, k = map(int, input().strip().split(' '))
    a = deque(map(int, input().strip().split(' ')))
    array_left_rotation(a, n, k);
    print(*a, sep=' ')
    

    And array_left_rotation only takes O(k) instead of O(n).

    Note that a is pased by reference, so there is no need to return anything, but this could be an issue for some user cases, for this particular problem, it works.