Advertisement
smj007

Untitled

Mar 6th, 2024 (edited)
910
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. def find_last_one(arr):
  2.     low, high = 0, len(arr) - 1
  3.     result = -1  # Initialize result to -1, in case there are no 1s in the array
  4.  
  5.     while low <= high:
  6.         mid = (low + high) // 2
  7.  
  8.         if arr[mid] == 1:
  9.             result = mid  # Update the result and continue searching on the right side
  10.             low = mid + 1
  11.         else:
  12.             high = mid - 1  # Search on the left side
  13.  
  14.     return result
  15.  
  16. # Example usage
  17. arr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  18. index_of_last_one = find_last_one(arr)
  19.  
  20. print("Index of the last occurrence of 1:", index_of_last_one)
  21.  
  22. def find_first_one(arr):
  23.     low, high = 0, len(arr) - 1
  24.     result = len(arr) - 1  # Initialize result to -1, in case there are no 1s in the array
  25.  
  26.     while low <= high:
  27.         mid = (low + high) // 2
  28.  
  29.         if arr[mid] == 1:
  30.             result = mid  # Update the result and continue searching on the left side
  31.             high = mid - 1
  32.         else:
  33.             low = mid + 1  # Search on the right side
  34.  
  35.     return result
  36.  
  37. # Example usage
  38. arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  39. index_of_first_one = find_first_one(arr)
  40.  
  41. print("Index of the first occurrence of 1:", index_of_first_one)
  42.  
Advertisement
Comments
  • Nenogzar
    1 year
    # Python 1.21 KB | 0 0
    1. ef find_last_one(arr):
    2.     low, high = 0, len(arr) - 1
    3.     result = -1  # Initialize result to -1, in case there are no 1s in the array
    4.  
    5.     while low <= high:
    6.         mid = (low + high) // 2
    7.  
    8.         if arr[mid] == 1:
    9.             result = mid  # Update the result and continue searching on the right side
    10.             low = mid + 1
    11.         else:
    12.             high = mid - 1  # Search on the left side
    13.  
    14.     return result
    15.  
    16.  
    17. # Example usage
    18. arr = []
    19. for i in range(11):
    20.     arr.append(1)
    21. for i in range(11, 22):
    22.     arr.append(0)
    23.  
    24. index_of_last_one = find_last_one(arr)
    25.  
    26. print("Index of the last occurrence of 1:", index_of_last_one)
    27.  
    28.  
    29. def find_first_one(arr):
    30.     low, high = 0, len(arr) - 1
    31.     result = -1  # Initialize result to -1, in case there are no 1s in the array
    32.  
    33.     while low <= high:
    34.         mid = (low + high) // 2
    35.  
    36.         if arr[mid] == 1:
    37.             result = mid  # Update the result and continue searching on the left side
    38.             high = mid - 1
    39.         else:
    40.             low = mid + 1  # Search on the right side
    41.  
    42.     return result
    43.  
    44.  
    45. # Example usage
    46. arr = arr[::-1]
    47. index_of_first_one = find_first_one(arr)
    48.  
    49. print("Index of the first occurrence of 1:", index_of_first_one)
Add Comment
Please, Sign In to add comment
Advertisement