Advertisement
This is comment for paste
Untitled
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ef find_last_one(arr):
- low, high = 0, len(arr) - 1
- result = -1 # Initialize result to -1, in case there are no 1s in the array
- while low <= high:
- mid = (low + high) // 2
- if arr[mid] == 1:
- result = mid # Update the result and continue searching on the right side
- low = mid + 1
- else:
- high = mid - 1 # Search on the left side
- return result
- # Example usage
- arr = []
- for i in range(11):
- arr.append(1)
- for i in range(11, 22):
- arr.append(0)
- index_of_last_one = find_last_one(arr)
- print("Index of the last occurrence of 1:", index_of_last_one)
- def find_first_one(arr):
- low, high = 0, len(arr) - 1
- result = -1 # Initialize result to -1, in case there are no 1s in the array
- while low <= high:
- mid = (low + high) // 2
- if arr[mid] == 1:
- result = mid # Update the result and continue searching on the left side
- high = mid - 1
- else:
- low = mid + 1 # Search on the right side
- return result
- # Example usage
- arr = arr[::-1]
- index_of_first_one = find_first_one(arr)
- print("Index of the first occurrence of 1:", index_of_first_one)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement