diff --git a/ FindPeakElement.java b/ FindPeakElement.java new file mode 100644 index 00000000..4f7045c6 --- /dev/null +++ b/ FindPeakElement.java @@ -0,0 +1,27 @@ +/** + * LeetCode 162: Find Peak Element + * https://leetcode.com/problems/find-peak-element/ + * + * This class provides a solution to the problem of finding a peak element in an array. + * A peak element is an element that is strictly greater than its neighbors. + * The algorithm uses binary search to achieve O(log n) time complexity. + */ +class Solution { + public int findPeakElement(int[] nums) { + + int start = 0; + int end = nums.length - 1; + while (start <= end) { + int mid = start + (end - start) / 2; + if( (mid ==0 || nums[mid]> nums[mid -1]) && (mid == nums.length-1 || nums[mid]> nums[mid+1])) + return mid; + else if(nums[mid+1] > nums[mid]) + start = mid+1; + else + end = mid -1; + + } + + return -1; + } +} \ No newline at end of file diff --git a/FindMinimumInRotatedSortedArray.java b/FindMinimumInRotatedSortedArray.java new file mode 100644 index 00000000..fd78427d --- /dev/null +++ b/FindMinimumInRotatedSortedArray.java @@ -0,0 +1,49 @@ +/** + * Leet code : https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ + * We wil use binary search to find the min in the rotated sorted array. + * Time Complexity = O(log n) + */ + +class FindMinimumInRotatedSortedArray { + + public int findMin(int[] nums) { + //check if array is not rotated + if (nums[0] <= nums[nums.length - 1]) return nums[0]; + + int start = 0; + int end = nums.length - 1; + int currentMin = nums[end]; + + while (start <= end) { + int mid = start + (end - start) / 2; + if (nums[mid] <= currentMin) { + currentMin = nums[mid]; + end = mid - 1; + } else + start = mid + 1; + } + return currentMin; + } + + public int findMin2(int[] nums) { + int n = nums.length; + int low = 0, high = n - 1; + + while (low <= high) { + if (nums[low] <= nums[high]) { + return nums[low]; + } + + int mid = low + (high - low) / 2; + if ((mid == 0 || nums[mid] < nums[mid - 1]) && (mid == n - 1 || nums[mid] < nums[mid + 1])) { + return nums[mid]; + } else if (nums[low] <= nums[mid]) { // left sorted range + low = mid + 1; + } else { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/FirstLastPositionOfElementInSortedArray.java b/FirstLastPositionOfElementInSortedArray.java new file mode 100644 index 00000000..abc13403 --- /dev/null +++ b/FirstLastPositionOfElementInSortedArray.java @@ -0,0 +1,44 @@ +/** + * Leet Code: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/ + * 1. We use binary search to find the fist and last index. + * 2. Use one binary search to find first occurrence and one more binary search to find last occurrence. + * 3. Time Complexity : 0(log n) + */ +class FirstLastPositionOfElementInSortedArray { + public int[] searchRange(int[] nums, int target) { + int[] result = {-1, -1}; + result[0] = binarySearch(nums, target, true); + result[1] = binarySearch(nums, target, false); + + return result; + } + + private int binarySearch(int[] nums, int target, boolean isSearchingLeftBound) { + int start = 0; + int end = nums.length - 1; + int idx = -1; + while (start <= end) { + int mid = start + (end - start) / 2; + if (nums[mid] == target) { + idx = mid; + if (isSearchingLeftBound) { + if (mid == 0 || nums[mid - 1] != target) { + return mid; + } else { + end = mid - 1; + } + } else { + if (mid == nums.length - 1 || nums[mid + 1] != target) { + return mid; + } else { + start = mid + 1; + } + } + } else if (nums[mid] > target) + end = mid - 1; + else + start = mid + 1; + } + return idx; + } +} \ No newline at end of file