diff --git a/P1_Better.java b/P1_Better.java new file mode 100644 index 00000000..c51917a5 --- /dev/null +++ b/P1_Better.java @@ -0,0 +1,61 @@ +class Solution { + + private int lowerBound(int[] nums, int target) { + int low = 0, high = nums.length - 1; + int ans = nums.length; + + while(low <= high) { + int mid = low + (high - low) / 2; + + if(nums[mid] >= target) { + ans = mid; + high = mid - 1; + } + else { + low = mid + 1; + } + } + return ans; + } + + private int upperBound(int[] nums, int target) { + int low = 0, high = nums.length - 1; + int ans = nums.length; + + while(low <= high) { + int mid = low + (high - low) / 2; + + if(nums[mid] > target) { + ans = mid; + high = mid - 1; + } + else { + low = mid + 1; + } + } + return ans; + } + + public int[] searchRange(int[] nums, int target) { + int firstOcc = lowerBound(nums, target); + + // Check if the target is present in the array or not + if(firstOcc == nums.length || nums[firstOcc] != target) return new int[]{-1, -1}; + + // Function call to find the last occurrence (upper bound) + int lastOcc = upperBound(nums, target) - 1; + + return new int[]{firstOcc, lastOcc}; + } +} + +// Time Complexity : 2*O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None + +/* Your code here along with comments explaining your approach in three sentences only - + +We use lower bound to find the first occurence of the element and the upper bound - 1 to get the last occurence of the element + +*/ diff --git a/P1_Brute.java b/P1_Brute.java new file mode 100644 index 00000000..afd83f85 --- /dev/null +++ b/P1_Brute.java @@ -0,0 +1,27 @@ +class Solution { + public int[] searchRange(int[] nums, int target) { + int first = -1, last = -1; + int[] ans = new int[2]; + int n = nums.length; + + for (int i = 0; i < n; i ++) { + if (nums[i] == target) { + if (first == -1) first = i; + last = i; + } + } + + ans[0] = first; + ans[1] = last; + return ans; + } +} + +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None + +/* Your code here along with comments explaining your approach in three sentences only - Will initise first and last variable to -1 and traverse the array to check if we have our target. + Once the target is met we store its index in both first and last variable and keep searching and updating the last variable with indexes. +*/ diff --git a/P1_Optimal.java b/P1_Optimal.java new file mode 100644 index 00000000..4909eb84 --- /dev/null +++ b/P1_Optimal.java @@ -0,0 +1,82 @@ +class Solution { + // Function to find the first occurrence of the target + private int firstOccurrence(int[] nums, int target) { + int low = 0, high = nums.length - 1; + int first = -1; + + // Applying Binary Search Algorithm + while(low <= high) { + int mid = low + (high - low) / 2; + + /* If the target element is found, we + update the first variable to mid and + eliminate the right half to look for + more smaller index where target is present */ + if(nums[mid] == target) { + first = mid; + high = mid - 1; + } + + /* If middle element is smaller, + we eliminate the left half */ + else if(nums[mid] < target) { + low = mid + 1; + } + + /* If middle element is greater, + we eliminate the right half */ + else { + high = mid - 1; + } + } + return first; + } + + // Function to find the last occurrence of the target + private int lastOccurrence(int[] nums, int target) { + int low = 0, high = nums.length - 1; + int last = -1; + + // Applying Binary Search Algorithm + while(low <= high) { + int mid = low + (high - low) / 2; + + /* If the target element is found, we + update the first variable to mid and + eliminate the right half to look for + more greater index where target is present */ + if(nums[mid] == target) { + last = mid; + low = mid + 1; + } + + /* If middle element is smaller, + we eliminate the left half */ + else if(nums[mid] < target) { + low = mid + 1; + } + + /* If middle element is greater, + we eliminate the right half */ + else { + high = mid - 1; + } + } + return last; + } + + // Function to find the first and last occurrences of the target + public int[] searchRange(int[] nums, int target) { + + // Function call to find the first occurence of target + int first = firstOccurrence(nums, target); + + // If the target is not present in the array + if(first == -1) return new int[]{-1, -1}; + + // Function call to find the last occurence of target + int last = lastOccurrence(nums, target); + + return new int[]{first, last}; + } +} \ No newline at end of file diff --git a/P2.java b/P2.java new file mode 100644 index 00000000..3160b522 --- /dev/null +++ b/P2.java @@ -0,0 +1,21 @@ +class Solution { + public int findMin(int[] nums) { + int n = nums.length; + int low = 0; + int high = n - 1; + + while (low < high) { + int mid = low + (high - low) / 2; + + // If the middle element is greater than the last element, + // the minimum is in the right part + if (nums[mid] > nums[high]) { + low = mid + 1; + } else { + // Otherwise, the minimum is in the left part (including mid) + high = mid; + } + } + return nums[low]; // At the end of the loop, `low == high`, pointing to the minimum + } +} diff --git a/P3.java b/P3.java new file mode 100644 index 00000000..345c2c2e --- /dev/null +++ b/P3.java @@ -0,0 +1,20 @@ +class Solution { + 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 > 0 && nums[mid - 1] > nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return 1; + } +}