Leetcode 704. Binary Search| Hindi | Python | Leetcode | Optimized way
Автор: DataEngineering360
Загружено: 2025-12-07
Просмотров: 7
#leetcode #python #timecomplexity
Q. Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Solution -
Optimized way - Note - this is sample code.. we cannot use angle brackets in description, so I removed them from the code.
class Solution:
def search(self, nums: List[int], target: int) - int:
l,r = 0, len(nums)-1
while l=r:
mid = (l+r)//2
if target == nums[mid]:
return mid
elif target nums[mid]:
r = mid - 1
else:
l = mid + 1
return -1
#python #leetcodesolutions #dsa #pythoninterviewquestions
Python Developer Interview Questions Answers
Top 10 Python Developer Interview Questions Answer
Mostly asked Python Developer Interview Questions Answer
Commonly asked Python Developer Interview Questions Answer
Tags -
Python leetcode problems
leetcode two sum
leetcode solutions
leetcode hindi
two sum python
two sum problem
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: