From ac18915e5ea1d9c66806b3fe4e8c57c5244512a9 Mon Sep 17 00:00:00 2001 From: Prashanthi Matam Date: Sat, 7 Feb 2026 00:34:19 -0800 Subject: [PATCH 1/3] Add findPeakElement function using binary search Implement binary search to find a peak element in an array. --- FindThePeakElement.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 FindThePeakElement.java diff --git a/FindThePeakElement.java b/FindThePeakElement.java new file mode 100644 index 00000000..a078a6f3 --- /dev/null +++ b/FindThePeakElement.java @@ -0,0 +1,27 @@ +# 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 +# Approach : if the array is raising the peak must be ahead and if the arrray is falling the peak from be behind . This lets us discard the half of the array each step using binary search + + + class Solution { + public int findPeakElement(int[] nums) { + int low = 0, high = nums.length - 1; + + while(low <= high){ + int mid = low + (high - low) / 2; + if(mid == low || nums[mid] > nums[mid - 1] && (mid == high || nums[mid] > nums[mid + 1])){ + return mid; + }else if(nums[mid + 1] > nums[mid]){ + low = mid + 1; + } + else{ + high = mid - 1; + } + + } + + return low; + } +} From 4c528a961a1287c1c574f89e9464d72706fa6bbc Mon Sep 17 00:00:00 2001 From: Prashanthi Matam Date: Sat, 7 Feb 2026 11:56:00 -0800 Subject: [PATCH 2/3] Add solution to find minimum in rotated sorted array Implemented a binary search algorithm to find the minimum element in a rotated sorted array. --- Find The Minimum in Rotated Sorted Array.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Find The Minimum in Rotated Sorted Array.java diff --git a/Find The Minimum in Rotated Sorted Array.java b/Find The Minimum in Rotated Sorted Array.java new file mode 100644 index 00000000..04954cf1 --- /dev/null +++ b/Find The Minimum in Rotated Sorted Array.java @@ -0,0 +1,29 @@ +# 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 +# Approach : One half of the array is always sorted The minimum element is the point where the sorted order breaks We can find that point using binary search in O(log n) time + +class Solution { + public int findMin(int[] nums) { + if(nums.length == 1){ + return nums[0]; + } + int left = 0, right = nums.length - 1; + if(nums[right] > nums[0]){ + return nums[0]; + } + int mid = 0; + while(left < right) { + mid = left + (right - left) / 2; + + if(nums[mid] < nums[right]) { + right = mid; + } else { + left = mid + 1; + } + } + + return nums[right]; + } +} From 9edc102f0c4f90cd1a9e009aa5c60c6b7af2159b Mon Sep 17 00:00:00 2001 From: Prashanthi Matam Date: Sat, 7 Feb 2026 12:09:02 -0800 Subject: [PATCH 3/3] Add binary search solution for target range Implemented a solution to find the first and last position of a target element in a sorted array using binary search. --- ...astPositionof Element in Sorted Array.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 FindFirstandLastPositionof Element in Sorted Array.java diff --git a/FindFirstandLastPositionof Element in Sorted Array.java b/FindFirstandLastPositionof Element in Sorted Array.java new file mode 100644 index 00000000..9989c789 --- /dev/null +++ b/FindFirstandLastPositionof Element in Sorted Array.java @@ -0,0 +1,58 @@ +Time Complexity: O(log n) because binary search is performed twice. + +Space Complexity: O(1) as no extra space is used. + +Did this code successfully run on Leetcode: Yes, it passed all test cases. + +Any problem you faced while coding this: No issues were encountered. + +Approach: # Use two modified binary searches to find the first and last occurrence of the target by narrowing the search range each time. + +class Solution { + public int[] searchRange(int[] nums, int target) { + int firstOccurrence = this.findBound(nums, target, true); + + if (firstOccurrence == -1) { + return new int[] { -1, -1 }; + } + + int lastOccurrence = this.findBound(nums, target, false); + + return new int[] { firstOccurrence, lastOccurrence }; + } + + private int findBound(int[] nums, int target, boolean isFirst) { + int N = nums.length; + int begin = 0, end = N - 1; + + while (begin <= end) { + int mid = (begin + end) / 2; + + if (nums[mid] == target) { + if (isFirst) { + // This means we found our lower bound. + if (mid == begin || nums[mid - 1] != target) { + return mid; + } + + // Search on the left side for the bound. + end = mid - 1; + } else { + // This means we found our upper bound. + if (mid == end || nums[mid + 1] != target) { + return mid; + } + + // Search on the right side for the bound. + begin = mid + 1; + } + } else if (nums[mid] > target) { + end = mid - 1; + } else { + begin = mid + 1; + } + } + + return -1; + } +}