From d9bc059fa9c4c89237baff12dca624868b5f251c Mon Sep 17 00:00:00 2001 From: Shreya Rathore Date: Sun, 16 Nov 2025 22:00:56 -0500 Subject: [PATCH 1/3] Completed exercise 1 --- MinimumInRotatedSortedArray.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 MinimumInRotatedSortedArray.java diff --git a/MinimumInRotatedSortedArray.java b/MinimumInRotatedSortedArray.java new file mode 100644 index 00000000..118d02c8 --- /dev/null +++ b/MinimumInRotatedSortedArray.java @@ -0,0 +1,25 @@ +// Time Complexity: O(log n) +// Space Complexity: O(1) + +// 1: We use Binary Search to find the minimum integer +// 2: We maintain a minimum value that is recalculated each time we change the range of the array +// 3: Once the condition on L#12 no longer holds true, we return the minimum value found +class Solution { + public int findMin(int[] nums) { + int low = 0; + int high = nums.length - 1; + int min = Integer.MAX_VALUE; + while(low <= high){ + int mid = (low + high)/2; + if(nums[low] <= nums[mid]){ + min = Math.min(min, nums[low]); + low = mid + 1; + } + else{ + min = Math.min(min, nums[mid]); + high = mid - 1; + } + } + return min; + } +} \ No newline at end of file From 11909fbe0b4e6404791c41539e8bcfb70d283095 Mon Sep 17 00:00:00 2001 From: Shreya Rathore Date: Sun, 16 Nov 2025 22:13:13 -0500 Subject: [PATCH 2/3] Completed exercise 2 --- FindPeakElement.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 FindPeakElement.java diff --git a/FindPeakElement.java b/FindPeakElement.java new file mode 100644 index 00000000..1666b8bc --- /dev/null +++ b/FindPeakElement.java @@ -0,0 +1,26 @@ +// Time Complexity: O(log n) +// Space Complexity: O(1) + +// 1: To find the peak element, we need to compare the mid element with its left and right elements +// 2: We will try to expand the range of search to the area where the element is larger +// 3: L#15 covers cases where mid is the first or last element +class Solution { + public int findPeakElement(int[] nums) { + int n = nums.length; + int low = 0; + int high = n - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + if ((mid == 0 || nums[mid] > nums[mid - 1]) + && (mid == n - 1 || nums[mid] > nums[mid + 1])) { + return mid; + } else if (nums[mid] < nums[mid + 1]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return -1; + } +} \ No newline at end of file From a5a1d6e764993dd85f87c17c1f9e88bbcf89ac28 Mon Sep 17 00:00:00 2001 From: Shreya Rathore Date: Sun, 16 Nov 2025 22:25:49 -0500 Subject: [PATCH 3/3] Completed exercise 3 --- FirstLastPositionInSortedArray.java | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 FirstLastPositionInSortedArray.java diff --git a/FirstLastPositionInSortedArray.java b/FirstLastPositionInSortedArray.java new file mode 100644 index 00000000..e7df987e --- /dev/null +++ b/FirstLastPositionInSortedArray.java @@ -0,0 +1,58 @@ +// Time Complexity: O(log n) +// Space Complexity: O(1) + +// 1: We perform binary search twice to find both the first and last index +// 2: To find the first index, we move the high pointer to mid-1 and then continue binary search as usual +// 3: To find the last index, the move the low pointer to mid+1 before continuing binary search +class Solution { + public int[] searchRange(int[] nums, int target) { + int size = nums.length - 1; + int first = binarySearchFirst(nums, target, 0, size); + + if (first == -1) return new int[]{-1, -1}; + + int last = binarySearchLast(nums, target, first, nums.length - 1); + + return new int[]{first, last}; + } + + private int binarySearchFirst(int[] nums, int target, int low, int high) { + + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] == target) { + if (mid == 0 || nums[mid - 1] != target) { + return mid; + } else { + high = mid - 1; + } + } else if (nums[mid] > target) { + high = mid - 1; + } else { + low = mid + 1; + } + } + + return -1; + } + + private int binarySearchLast(int[] nums, int target, int low, int high) { + + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] == target) { + if (mid == size || nums[mid + 1] != target) { + return mid; + } else { + low = mid + 1; + } + } else if (nums[mid] > target) { + high = mid - 1; + } else { + low = mid + 1; + } + } + + return -1; + } +} \ No newline at end of file