diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..6fcb3f5c --- /dev/null +++ b/Problem1.java @@ -0,0 +1,53 @@ +// Time Complexity : O(log n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this : Confused about the situations where we have to return [-1, -1]. +// Approach: Perform 2 binasry searches. 1 for finding the first index of the target element and 2 for the last index for the target element +// If the target is not found in the first binary search than return [-1, -1] +// Or else, find the last index of the target by erformong the second binary search and return the indexes. +class Problem1 { + private int bs1(int [] arr, int low, int high, int target){ + while(low <= high){ + int mid = low + (high - low)/2; + if(arr[mid] == target){ + if(mid == 0 || arr[mid] > arr[mid -1]){ + return mid; + } else{ + high = mid -1; + } + } else if(arr[mid] > target){ + high = mid - 1; + } else{ + low = mid + 1; + } + } + return -1; + } + private int bs2(int [] arr, int low, int high, int target){ + while(low <= high){ + int mid = low + (high - low)/2; + if(arr[mid] == target){ + if(mid == arr.length-1 || arr[mid+1] > arr[mid]){ + return mid; + } else{ + low = mid + 1; + } + } else if(arr[mid] > target){ + high = mid - 1; + } else{ + low = mid + 1; + } + } + return -1; + } + public int[] searchRange(int[] nums, int target) { + int n = nums.length; + if(nums == null || n == 0) return new int[]{-1, -1}; + if(target < nums[0]) return new int[]{-1, -1}; + if(target > nums[n-1]) return new int[]{-1, -1}; + int first = bs1(nums, 0, n-1, target); + if(first == -1) return new int[]{-1, -1}; + int second = bs2(nums, first, n-1, target); + return new int[]{first, second}; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..c238c73d --- /dev/null +++ b/Problem2.java @@ -0,0 +1,31 @@ +//Time Complexity: O(logn) +// Space Complexity: O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Forgot to take care of edge cases +// Approach: Perform binary search to find the pivot +// Compare the elements at low and high to find out if the array is sorted or not +// Otherwise check which side of the array is sorted and adjust the values of low and accordingly till you find the minimum element +class Problem2 { + public int findMin(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]){ + low = mid + 1; + }else{ + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..383128e7 --- /dev/null +++ b/Problem3.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 : Was coinfused a bit in writing the if conditions and edge cases +// Approach: Perform a binary search then compare mid with the neighbors if it is greater then retirn mid which will be the peak +// If not then move to the side which has the greater number then mid if the number to right of the mid is greater than move right otherwise left +// The binary search continues till the peak element is found +class Problem3 { + 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(mid != n-1 && nums[mid + 1] > nums[mid]){ + low = mid + 1; + }else{ + high = mid - 1; + } + } + return 113; + } +} \ No newline at end of file