Left Rotation

  • + 0 comments

    Ugly Python3 in-place solution: O(N) time, O(1) space

    def rotateLeft(d, arr, lo = 0):
        n = len(arr) - lo
        d = d % n
        if d == 0:
            return arr
        r = n - d
            
        for i in range(r):
            arr[lo + i], arr[lo + d + i] = arr[lo + d + i], arr[lo + i]
        return rotateLeft(d - r, arr, lo + r)