-
Notifications
You must be signed in to change notification settings - Fork 0
/
broken_array.py
42 lines (33 loc) · 874 Bytes
/
broken_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
ID 89009319
"""
def broken_search(nums, target) -> int:
low = 0
high = len(nums) - 1
n = len(nums)
while low <= high:
mid = (low + high) // 2
mid_value = nums[mid]
if mid_value == target:
return mid
if nums[low] <= nums[mid]:
if nums[low] <= target < nums[mid]:
high = mid - 1
else:
low = mid + 1
else:
if nums[mid] < target <= nums[high]:
low = mid + 1
else:
high = mid - 1
return -1
def test():
arr = [19, 21, 100, 101, 1, 4, 5, 7, 12]
assert broken_search(arr, 5) == 6
def main():
array_len = int(input())
number = int(input())
array = list(map(int, input().strip().split()))
print(broken_search(array, number))
if __name__ == "__main__":
test()