diff --git a/FindFirstandLastpositionofElementinSortedArray.java b/FindFirstandLastpositionofElementinSortedArray.java new file mode 100644 index 00000000..bfe9d358 --- /dev/null +++ b/FindFirstandLastpositionofElementinSortedArray.java @@ -0,0 +1,61 @@ +// 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 + + +// Your code here along with comments explaining your approach +/* +We compute binary searches for left and right part of array to understand the first and last occurences +We make sure to check if mid value equals to the target element and has a chance of becoming first/last +occurence at validations checks accordingly. If not, we move our high/low positions to left and right +as required to find. + */ +class Solution { + public int[] searchRange(int[] nums, int target) { + if(nums.length == 0) + return new int[]{-1, -1}; + + int first = binarySearchFirst(nums, target, 0, nums.length - 1); + + if(first == -1) + return new int[]{-1, -1}; + + int second = binarySearchSecond(nums, target, first, nums.length - 1); + return new int[]{first, second}; + } + + 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 == low || nums[mid] != nums[mid - 1]) + return mid; + else + high = mid - 1; + } + else if(nums[mid] > target) + high = mid - 1; + else + low = mid + 1; + } + return -1; + } + + private int binarySearchSecond(int[] nums, int target, int low, int high) { + while(low <= high) { + int mid = low + (high - low) / 2; + if(target == nums[mid]) { + if(mid == high || nums[mid] != nums[mid + 1]) + 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 diff --git a/FindMinimuminRotatedSortedArray.java b/FindMinimuminRotatedSortedArray.java new file mode 100644 index 00000000..87f28ba1 --- /dev/null +++ b/FindMinimuminRotatedSortedArray.java @@ -0,0 +1,31 @@ +// 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 + + +// Your code here along with comments explaining your approach +/* +we perform binary search to find min element by intially trying to check base case of if this is a sorted +array, then directly return low value. If not, find mid value and check mid's base case check. If not, +check if the value falls in right sorted range by comparing mid value with high value and updating high, +low values accordingly. + */ +class Solution { + public int findMin(int[] nums) { + int low = 0, high = nums.length - 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]) && (nums[mid] < nums[mid + 1])) + return nums[mid]; + else if(nums[mid] <= nums[high]) + high = mid - 1; + else + low = mid + 1; + } + return -1; + } +} \ No newline at end of file diff --git a/FindPeakElement.java b/FindPeakElement.java new file mode 100644 index 00000000..d14e7341 --- /dev/null +++ b/FindPeakElement.java @@ -0,0 +1,28 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +/* +We implement binary search by checking if value at mid is greater than previous value and also next index's +value by making sure of edge case checks. If so, we return the index of mid, if not, we move low and high +indices accordingly. + */ +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 == 0 || 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 -1; + } +} \ No newline at end of file