Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Time Complexity : O(log(m * n))
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : NA

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# Approach: Treat the 2D matrix as a 1D sorted array to apply binary search
m = len(matrix)
n = len(matrix[0])
low = 0 # Initialize the low pointer
high = m * n - 1 # High pointer is the last element index (total elements - 1)

while low <= high:
mid = low + (high - low) // 2 # Calculate mid index
row = mid // n # Convert 1D mid index to row index
column = mid % n # Convert 1D mid index to column index

# Check if the mid element matches the target
if matrix[row][column] == target:
return True
# If mid element is smaller than target, search the right half
elif matrix[row][column] < target:
low = mid + 1
# If mid element is greater than target, search the left half
else:
high = mid - 1

# Target not found
return False
38 changes: 38 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Time Complexity : O(log n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : NA

class Solution:
def search(self, nums: List[int], target: int) -> int:
"""
:type nums: List[int]
:type target: int
:rtype: int
"""

left = 0 # Initialize left pointer
right = len(nums) - 1 # Initialize right pointer (last index)

while left <= right:
mid = (left + right) // 2 # Calculate mid index

if nums[mid] == target: # Check if the mid element matches the target, return index
return mid

# Check if the left half is sorted
if nums[left] <= nums[mid]:
# If target lies within the left sorted half
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
# Right half is sorted
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1

# Target not found
return -1
32 changes: 32 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# Time Complexity : O(log(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : NA(Premium program)
# Any problem you faced while coding this : NA
def search( arr, target: int) -> int:
# Approach: Expand the search boundary exponentially until 'high' is greater than target
low, high = 0, 1
while arr[high]< target:
low =high
high*=2
while(low<=high):
mid = (low+high)//2 # Calculate mid index
# Check if the mid element matches the target
if arr[mid]==target:
return mid
# If mid element is smaller than target, search the right half
elif arr[mid]< target:
low= mid+1

# If mid element is greater than target, search the left half

else:
high = mid -1
# Target not found
return -1





print(search([1,3,4,5,6,7,8,34,67,89,100,237, 268, 300,344, 566,799,800], 11))