From adbe65989e73af7eea7d6af18b7a863ebaf8cc0a Mon Sep 17 00:00:00 2001 From: Subba Paritala Date: Tue, 5 May 2026 11:26:31 -0500 Subject: [PATCH] Complete Binary Search 1 --- search_2d_matrix.py | 43 +++++++++++++++++ search_in_rotated_sorted_array.py | 33 +++++++++++++ search_in_sorted_array_unknow_size.py | 68 +++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 search_2d_matrix.py create mode 100644 search_in_rotated_sorted_array.py create mode 100644 search_in_sorted_array_unknow_size.py diff --git a/search_2d_matrix.py b/search_2d_matrix.py new file mode 100644 index 00000000..5fe8f7e7 --- /dev/null +++ b/search_2d_matrix.py @@ -0,0 +1,43 @@ +""" +Find the row of the target element first +and then using that row do binary search to find the target +Time Complexity : O(log mn ) +Space Complexity : O(1) +""" +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + + top = 0 + bottom = len(matrix) - 1 + + + while top <= bottom: + mid = (top + bottom) // 2 + + if target > matrix[mid][-1]: + top = mid + 1 + elif target < matrix[mid][0]: + bottom = mid - 1 + else: + break + + + row = (top + bottom) // 2 + + l, r = 0, len(matrix[0]) - 1 + + while l <= r: + + mid = (l + r) // 2 + + if target > matrix[row][mid]: + l = mid + 1 + elif target < matrix[row][mid]: + r = mid - 1 + else: + return True + + return False + + + \ No newline at end of file diff --git a/search_in_rotated_sorted_array.py b/search_in_rotated_sorted_array.py new file mode 100644 index 00000000..5e783d33 --- /dev/null +++ b/search_in_rotated_sorted_array.py @@ -0,0 +1,33 @@ +""" +Check the array is left sorted or right sorted array and then check if the target +is present in the left half or right + +Time Complexity: O(log n) +Space Complexity: O(1) +""" +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 nums[mid] == target: + return mid + + # Left sorted + if nums[l] <= nums[mid]: + + if nums[l] <= target and nums[mid] > target: + r = mid - 1 + else: + l = mid + 1 + else: + # Right sorted + if nums[mid] < target and nums[r] >= target: + l = mid + 1 + else: + r = mid - 1 + return -1 diff --git a/search_in_sorted_array_unknow_size.py b/search_in_sorted_array_unknow_size.py new file mode 100644 index 00000000..cea96023 --- /dev/null +++ b/search_in_sorted_array_unknow_size.py @@ -0,0 +1,68 @@ +""" +This is an interactive problem. + +You have a sorted array of unique elements and an unknown size. You do not have an access to the array but you can use the ArrayReader interface to access it. You can call ArrayReader.get(i) that: + +returns the value at the ith index (0-indexed) of the secret array (i.e., secret[i]), or +returns 231 - 1 if the i is out of the boundary of the array. +You are also given an integer target. + +Return the index k of the hidden array where secret[k] == target or return -1 otherwise. + +You must write an algorithm with O(log n) runtime complexity. + + + +Example 1: + +Input: secret = [-1,0,3,5,9,12], target = 9 +Output: 4 +Explanation: 9 exists in secret and its index is 4. +Example 2: + +Input: secret = [-1,0,3,5,9,12], target = 2 +Output: -1 +Explanation: 2 does not exist in secret so return -1. + +Use the helper method and move the right pointer until the target is greater than the right pointer +move left pointer to right pointer value and move left pointer to prev left pointer multiple with 2 + +After getting the left and right pointer do normal binary search + +Time Complexity: O(log n) +Space Complexity: O(1) +""" + + +# """ +# This is ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class ArrayReader: +# def get(self, index: int) -> int: + +class Solution: + def search(self, reader: 'ArrayReader', target: int) -> int: + l = 0 + r = 1 + while l <= r: + + if reader.get(r) > target: + break + + l = r + r = 2 * r + + while l <= r: + + mid = (l + r) // 2 + + if reader.get(mid) == target: + return mid + + if reader.get(mid) > target: + r = mid - 1 + else: + l = mid + 1 + + return -1 \ No newline at end of file