diff --git a/2024_Problem10_Find_Minimum_in_Rotated_Array.java b/2024_Problem10_Find_Minimum_in_Rotated_Array.java new file mode 100644 index 00000000..562f117c --- /dev/null +++ b/2024_Problem10_Find_Minimum_in_Rotated_Array.java @@ -0,0 +1,30 @@ +//153. Find Min in Rotated Array - https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ +//Time Complexity: O(n) +//Space Complexity: O(1) + +class Solution { + public int findMin(int[] nums) { + + int low = 0, high = nums.length-1; + + while(low<=high){ + //if we are in sorted part of the array + if(nums[low] < nums[high]) + return nums[low]; + //calculate mid + int mid = low + (high-low)/2; + //check if mid is the minimum + if((mid == low || nums[mid] < nums[mid-1]) && + (mid == high || nums[mid] < nums[mid+1])){ + return nums[mid]; + } + //sorted side - move to unsorted part + else if(nums[low] <= nums[mid]){ //Right sorted + low = mid+1; + }else { + high=mid-1; //left sorted + } + } + return -1; + } +} \ No newline at end of file diff --git a/2024_Problem11_Find_peak_Element.java b/2024_Problem11_Find_peak_Element.java new file mode 100644 index 00000000..0840aae9 --- /dev/null +++ b/2024_Problem11_Find_peak_Element.java @@ -0,0 +1,26 @@ +//162. Find Peak Element - https://leetcode.com/problems/find-peak-element/description/ +//Time Complexity: log(n) +//Space Complexity: O(1) + +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-1] < nums[mid]) && + (mid == high || nums[mid+1] < nums[mid])){ + return mid; + } + //moving to the greater side + else if(nums[mid] < nums[mid+1]){ + low = mid + 1; + } + //moving to the greater side + else if(nums[mid] > nums[mid+1]){ + high = mid - 1; + } + } + return -1; + } +} \ No newline at end of file diff --git a/2024_Problem9_Find_First_and_Last_Position_of_Element_in_Sorted_Array.java b/2024_Problem9_Find_First_and_Last_Position_of_Element_in_Sorted_Array.java new file mode 100644 index 00000000..a307c5d5 --- /dev/null +++ b/2024_Problem9_Find_First_and_Last_Position_of_Element_in_Sorted_Array.java @@ -0,0 +1,56 @@ +//34. Find First and Last position of an Element in Sorted Array - https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ +//Time Complexity: O(2log(n)) ~ log(n) +//Space Complexity: O(1) + +class Solution { + + //to get first index + public int binarySearchLeft(int[] nums, int target) { + int low = 0; int high = nums.length - 1; + while(low <= high){ + int mid = low + (high - low)/2; + if(nums[mid] == target){ + if(mid == 0 || 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; + } + + //to get last index + public int binarySearchRight(int[] nums, int target) { + int low = 0; int high = nums.length - 1; + while(low <= high){ + int mid = low + (high - low)/2; + if(nums[mid] == target){ + if(mid == nums.length -1 || 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; + } + + public int[] searchRange(int[] nums, int target) { + //base case + if(nums == null || nums.length == 0){ + return new int[]{-1,-1}; + } + int first = binarySearchLeft(nums, target); + int last = binarySearchRight(nums, target); + return new int[]{first, last}; + } +} \ No newline at end of file