Arrays: Left Rotation

  • + 2 comments

    True. However, it becomes an elegant solution if we use collections.dequeue instead of list. Double ended queues have a popleft method, which is an O(1) operation:

    def array_left_rotation(a, n, k):
        for _ in range(k):
            a.append(a.popleft())
        return a
    

    More info: https://wiki.python.org/moin/TimeComplexity#collections.deque https://docs.python.org/3/library/collections.html#collections.deque