Skip to content

Instantly share code, notes, and snippets.

@kunal768
Created April 12, 2024 22:38
Show Gist options
  • Save kunal768/66856c3d2aaf02ccdbdd2eb4e5b04e22 to your computer and use it in GitHub Desktop.
Save kunal768/66856c3d2aaf02ccdbdd2eb4e5b04e22 to your computer and use it in GitHub Desktop.
Binary Search - New Way
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
lo, hi = 0, n - 1
while hi - lo > 1 :
mid = (lo + hi)//2
if nums[mid] < target :
lo = mid + 1
else:
hi = mid
if nums[lo] == target : return lo
elif nums[hi] == target : return hi
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment