Guest User

Untitled

a guest
Jun 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #def index_equals_value_search(arr):
  2. #pass # your code goes here
  3. # for i in range(0,len(arr)):
  4. # if arr[i] == i:
  5. # return i
  6.  
  7. # return -1
  8.  
  9.  
  10. def binarySearch(arr,l,r):
  11.  
  12. if l > r:
  13. return -1
  14.  
  15. m = (l + r)/2
  16. currentIndex = -1
  17.  
  18. if arr[m] == m:
  19.  
  20. currentIndex = m
  21. binarySearch(arr,l,m-1)
  22.  
  23. if arr[m] < m:
  24. return binarySearch(arr,m+1,r)
  25.  
  26. if arr[m] > m:
  27. return binarySearch(arr,l,m-1)
  28.  
  29.  
  30. return currentIndex
  31.  
  32.  
  33. def index_equals_value_search(arr):
  34. #pass # your code goes here
  35. # edge cases:
  36. if len(arr) == 0:
  37. return -1
  38.  
  39. return binarySearch(arr,0,len(arr)-1)
Add Comment
Please, Sign In to add comment