fork download
  1. import random
  2.  
  3. def print_longest_consecutive_zeros(k, l):
  4. # Create an array to store the binary string
  5. binary_string = [0] * l
  6.  
  7. # Place k ones in the array at random positions
  8. for _ in range(k):
  9. random_position = random.randint(0, l - 1)
  10. # Ensure the random position is not already occupied
  11. while binary_string[random_position] == 1:
  12. random_position = random.randint(0, l - 1)
  13. binary_string[random_position] = 1
  14.  
  15. # Convert the array to a string
  16. binary_string = ''.join(map(str, binary_string))
  17.  
  18. # Calculate the length of the longest consecutive zeros
  19. max_zeros = 0
  20. current_zeros = 0
  21.  
  22. for char in binary_string:
  23. if char == '0':
  24. current_zeros += 1
  25. if current_zeros > max_zeros:
  26. max_zeros = current_zeros
  27. else:
  28. current_zeros = 0
  29.  
  30. # Print the result
  31. print(f"Length of the longest consecutive zeros: {max_zeros}")
  32.  
  33. if __name__ == "__main__":
  34. # Get input for the number of ones (k) and length of binary string (l)
  35. k = int(input("Enter the number of ones (k): "))
  36. l = int(input("Enter the length of binary string (l): "))
  37.  
  38. # Call the function to print the result
  39. print_longest_consecutive_zeros(k, l)
  40.  
Success #stdin #stdout 0.02s 26044KB
stdin
3
3
stdout
import random

def print_longest_consecutive_zeros(k, l):
    # Create an array to store the binary string
    binary_string = [0] * l

    # Place k ones in the array at random positions
    for _ in range(k):
        random_position = random.randint(0, l - 1)
        # Ensure the random position is not already occupied
        while binary_string[random_position] == 1:
            random_position = random.randint(0, l - 1)
        binary_string[random_position] = 1

    # Convert the array to a string
    binary_string = ''.join(map(str, binary_string))

    # Calculate the length of the longest consecutive zeros
    max_zeros = 0
    current_zeros = 0

    for char in binary_string:
        if char == '0':
            current_zeros += 1
            if current_zeros > max_zeros:
                max_zeros = current_zeros
        else:
            current_zeros = 0

    # Print the result
    print(f"Length of the longest consecutive zeros: {max_zeros}")

if __name__ == "__main__":
    # Get input for the number of ones (k) and length of binary string (l)
    k = int(input("Enter the number of ones (k): "))
    l = int(input("Enter the length of binary string (l): "))

    # Call the function to print the result
    print_longest_consecutive_zeros(k, l)