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]; + } +} 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; + } +} 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; + } +}