Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #def index_equals_value_search(arr):
- #pass # your code goes here
- # for i in range(0,len(arr)):
- # if arr[i] == i:
- # return i
- # return -1
- def binarySearch(arr,l,r):
- if l > r:
- return -1
- m = (l + r)/2
- currentIndex = -1
- if arr[m] == m:
- currentIndex = m
- binarySearch(arr,l,m-1)
- if arr[m] < m:
- return binarySearch(arr,m+1,r)
- if arr[m] > m:
- return binarySearch(arr,l,m-1)
- return currentIndex
- def index_equals_value_search(arr):
- #pass # your code goes here
- # edge cases:
- if len(arr) == 0:
- return -1
- return binarySearch(arr,0,len(arr)-1)
Add Comment
Please, Sign In to add comment