Left Rotation

  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def left_rotation(d, arr):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(n)
        new_array = [0] * len(arr)
        for index in range(0, len(arr)):
            new_array[(index - d) % len(arr)] = arr[index]
    
        return new_array
    
    
    def left_rotation_in_place(d, arr):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(1)
        n = len(arr)
        start_range = 0
        end_range = d
        for index in range(0, (end_range - start_range) // 2):
            arr[index + start_range], arr[end_range - index - 1] = (
                arr[end_range - index - 1],
                arr[index + start_range],
            )
    
        start_range = d
        end_range = n
        for index in range(0, (end_range - start_range) // 2):
            arr[index + start_range], arr[end_range - index - 1] = (
                arr[end_range - index - 1],
                arr[index + start_range],
            )
    
        start_range = 0
        end_range = n
        for index in range(0, (end_range - start_range) // 2):
            arr[index + start_range], arr[end_range - index - 1] = (
                arr[end_range - index - 1],
                arr[index + start_range],
            )
    
        return arr