From be2995b6f77e8cec5ecfccc288d5434c36fe9acd Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Sun, 28 Dec 2025 10:28:59 -0500 Subject: [PATCH] Done Binary-Search-2 --- PeakElement.py | 23 ++++++++++++++++ findFirstLastSortedArray.py | 55 +++++++++++++++++++++++++++++++++++++ findMinSortedArray.py | 43 +++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 PeakElement.py create mode 100644 findFirstLastSortedArray.py create mode 100644 findMinSortedArray.py diff --git a/PeakElement.py b/PeakElement.py new file mode 100644 index 00000000..7350489a --- /dev/null +++ b/PeakElement.py @@ -0,0 +1,23 @@ +''' Time Complexity : O(log n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Your code here along with comments explaining your approach + + Approach : Comparing if mid greater than its both neighbors, return mid + Else , slide towards greater neighbor +''' +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + n = len(nums) + l, h = 0, n-1 + while l <= h: + mid = (l+h) // 2 + print(l,h,mid) + if ((mid == 0 or nums[mid] > nums[mid-1]) and (mid == n-1 or nums[mid] > nums[mid+1])): + return mid + elif nums[mid+1] > nums[mid]: + l = mid + 1 + else: + h = mid - 1 \ No newline at end of file diff --git a/findFirstLastSortedArray.py b/findFirstLastSortedArray.py new file mode 100644 index 00000000..8e7fa9ad --- /dev/null +++ b/findFirstLastSortedArray.py @@ -0,0 +1,55 @@ +''' Time Complexity : O(log n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Your code here along with comments explaining your approach + + Approach : Implementing 2 binray search - for First and Last index + 1. Find First index by comparing with [mid-1], + 2. Use first index as low for Second binary search + 3. Find Second index by comparing with [mid+1] + +''' + +class Solution: + def searchRange(self, nums: List[int], target: int) -> List[int]: + + def firstBinarySearch(nums,l,h): + while l <= h: + mid = (l+h)//2 + print(l, h , mid) + if nums[mid] == target: + if mid == 0 or nums[mid-1] != target: + return mid + else: + h = mid - 1 + + elif nums[mid] < target: + l = mid + 1 + else: + h = mid - 1 + return -1 + + def lastBinarySearch(nums,l,h): + while l <= h: + mid = (l+h)//2 + if nums[mid] == target: + if mid == n-1 or nums[mid+1] != target: + return mid + else: + l = mid + 1 + + elif nums[mid] < target: + l = mid + 1 + else: + h = mid - 1 + return -1 + + n = len(nums) + first = firstBinarySearch(nums,0,n-1) + if first == -1: + return [-1,-1] + last = lastBinarySearch(nums,first,n-1) + return [first,last] + \ No newline at end of file diff --git a/findMinSortedArray.py b/findMinSortedArray.py new file mode 100644 index 00000000..c1038b5f --- /dev/null +++ b/findMinSortedArray.py @@ -0,0 +1,43 @@ +''' Time Complexity : O(log n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Your code here along with comments explaining your approach + +''' +# --- Solution 1 : ------ +''' Approach : Comparing if mid less than its neighbours, return mid + Else , find the sorted array and move to opposite direction +''' +class Solution: + def findMin(self, nums: List[int]) -> int: + n = len(nums) + l, h = 0, n-1 + while l <= h: + if nums[l] <= nums[h]: + return nums[l] + mid = (l+h) // 2 + if nums[mid] < nums[mid-1] and nums[mid] < nums[mid+1]: + return nums[mid] + elif nums[l] <= nums[mid]: + l = mid + 1 + else: + h = mid -1 + +# --- Solution 2 : ------ +''' Approach : If mid is greater than high, means left side is sorted + and min element lies in right side. +''' +class Solution: + def findMin(self, nums: List[int]) -> int: + n = len(nums) + l, h = 0, n-1 + while l < h: + mid = (l + h) // 2 + if nums[mid] > nums[h]: + # left side is sorted and min lies in right side + l = mid + 1 + else: + h = mid + return nums[l] \ No newline at end of file