Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed BinarySearch-2 Problems #1906

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 2024_Problem10_Find_Minimum_in_Rotated_Array.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 26 additions & 0 deletions 2024_Problem11_Find_peak_Element.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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};
}
}