Skip to content

Commit 76100ed

Browse files
committed
Value equal to index value
1 parent 71a050b commit 76100ed

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Apple/Problem#273.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
A fixed point in an array is an element whose value is equal to its index.
3+
Given a sorted array of distinct elements, return a fixed point, if one exists. Otherwise, return False.
4+
For example, given [-6, 0, 2, 40], you should return 2. Given [1, 5, 7, 8], you should return False.
5+
"""
6+
def fixed_point(arr):
7+
start, end = 0, len(arr)-1
8+
while start <= end:
9+
mid = (start+end)//2
10+
if arr[mid] == mid:
11+
return mid
12+
elif arr[mid] < mid:
13+
start = mid + 1
14+
else:
15+
end = mid - 1
16+
return False

0 commit comments

Comments
 (0)