diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..c022086a --- /dev/null +++ b/Problem-1.py @@ -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 diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..5209bfc0 --- /dev/null +++ b/Problem-2.py @@ -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 diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 00000000..a1196fc8 --- /dev/null +++ b/Problem-3.py @@ -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)) \ No newline at end of file