diff --git a/find-first-and-last-position-of-element-in-sorted-array.java b/find-first-and-last-position-of-element-in-sorted-array.java new file mode 100644 index 00000000..7d5eaf64 --- /dev/null +++ b/find-first-and-last-position-of-element-in-sorted-array.java @@ -0,0 +1,64 @@ +// 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: +We perform binary search twice — first to find the first occurrence and then to find the last occurrence of the target. +In the first search, once we find the target, we continue searching on the left to ensure it is the first index, and similarly search right for the last occurrence. +If the target is not found, we return [-1, -1]; otherwise, we return the indices of first and last occurrence. +*/ + +class Solution { + + private int binarySearchFirst(int[] nums, int target, int low, int high) { + while (low <= high) { + int mid = low + (high - low) / 2; + + if (target == nums[mid]) { + if (mid == 0 || nums[mid - 1] != target) { + return mid; + } else { + high = mid - 1; + } + } else if (target < nums[mid]) { + 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 (target == nums[mid]) { + if (mid == nums.length - 1 || nums[mid + 1] != target) { + return mid; + } else { + low = mid + 1; + } + } else if (target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return -1; + } + + public int[] searchRange(int[] nums, int target) { + int first = binarySearchFirst(nums, target, 0, nums.length - 1); + + if (first == -1) { + return new int[]{-1, -1}; + } + + int last = binarySearchLast(nums, target, first, nums.length - 1); + + return new int[]{first, last}; + } +} \ No newline at end of file diff --git a/find-minimum-in-rotated-sorted-array.java b/find-minimum-in-rotated-sorted-array.java new file mode 100644 index 00000000..a1ebab5c --- /dev/null +++ b/find-minimum-in-rotated-sorted-array.java @@ -0,0 +1,45 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes (for no-duplicate version) +// Any problem you faced while coding this : No + +/* +Approach: +We use binary search to find the minimum element (pivot) in a rotated sorted array by identifying the unsorted half. +If the current range is already sorted, we directly return nums[low], otherwise we check if mid is the pivot by comparing neighbors. +If left half is sorted, we move right; otherwise, we move left to continue searching for the minimum. +*/ + +class Solution { + public int findMin(int[] nums) { + int low = 0; + int high = nums.length - 1; + + while (low <= high) { + // If already sorted, the smallest element is at low + if (nums[low] <= nums[high]) { + return nums[low]; + } + + int mid = low + (high - low) / 2; + + // Check if mid is the minimum (pivot) + if ((mid == 0 || nums[mid] < nums[mid - 1]) && + (mid == nums.length - 1 || nums[mid] < nums[mid + 1])) { + return nums[mid]; + } + + // Left half is sorted → pivot must be in right half + else if (nums[low] <= nums[mid]) { + low = mid + 1; + } + + // Right half is sorted → pivot must be in left half + else { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/find-peak-element.java b/find-peak-element.java new file mode 100644 index 00000000..48b66d11 --- /dev/null +++ b/find-peak-element.java @@ -0,0 +1,39 @@ +// 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 (3 sentences): +We use binary search to find a peak element by checking if the current mid is greater than its neighbors. +If the right neighbor is greater, it means we are on an increasing slope, so a peak must exist on the right side; otherwise, it lies on the left. +This works because a peak always exists, and we reduce the search space based on the slope direction. +*/ + +class Solution { + public int findPeakElement(int[] nums) { + int low = 0; + int high = nums.length - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + + // Check if mid is a peak + if ((mid == 0 || nums[mid] > nums[mid - 1]) && + (mid == nums.length - 1 || nums[mid] > nums[mid + 1])) { + return mid; + } + + // Increasing slope → peak on right + else if (nums[mid] < nums[mid + 1]) { + low = mid + 1; + } + + // Decreasing slope → peak on left + else { + high = mid - 1; + } + } + return -1; + } +} \ No newline at end of file