-
Notifications
You must be signed in to change notification settings - Fork 3
/
58_Amazon_Find_Element_In_Rotated_Array.py
executable file
·49 lines (35 loc) · 1.5 KB
/
58_Amazon_Find_Element_In_Rotated_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
43
44
45
46
47
48
49
"""
This problem was asked by Amazon.
An sorted array of integers was rotated an unknown number of times.
Given such an array, find the index of the element in the array in faster than linear time.
If the element doesn't exist in the array, return null.
For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 (the index of 8 in the array).
You can assume all the integers in the array are unique.
"""
def find_element_pos(arr, element):
def helper(arr, element, start, end):
if end < start:
return
mid = start + ((end - start) //2)
if arr[mid] == element:
return mid
if arr[mid] > arr[start]:
# means the array start->mid is not rotated, completely sorted
if arr[start] <= element <= arr[mid]:
# the element must be in lower half
return helper(arr, element, start, mid-1)
else:
# element must be in upper half
return helper(arr, element, mid+1, end)
else:
# elements from mid->end
# must be sorted
if arr[mid] <= element <= arr[end]:
# element in upper half
return helper(arr, element, mid+1, end)
else:
# otherwise, must be in lower half
return helper(arr, element, start, mid-1)
return helper(arr, element, start=0, end=len(arr)-1)
if __name__ == '__main__':
print(find_element_pos([13, 18, 25, 2, 8, 10] , 2))