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

Binary-Search-2 Completed #1916

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
52 changes: 52 additions & 0 deletions FindMinInSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity: O(logn)
// Space Complexity : O(1)
// Approach : Apply Binary Search to reduce the search range by moving the pointer towards unsorted portion as chances of min on the sorted portion is less.
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;
// array rotated n times then it becomes sorted array
if(nums[low]<=nums[high]){
return nums[low];
}
// mid happen to be the pivot where the rotation happened
if((mid == 0 || (nums[mid-1] > nums[mid])) && (mid==high || (nums[mid+1] > nums[mid]))){
return nums[mid];
// right Array is sorted
}else if(nums[mid]<nums[high]){
high=mid-1;
// left Array is sorted
}else{
low=mid+1;
}
}
return -1;
}
}
class Solution {
// Time complexity: O(logn)
// Space Complexity: O(1)
// Approach : Binary Search and divide the search space by eliminating the sorted portion because min wont be present.
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((mid==0 || nums[mid-1]>nums[mid]) && (mid == high || nums[mid+1]>nums[mid])){
return nums[mid] ;// on the pivot - rotation happend
}
// sorted portion is on right
if(nums[mid]<nums[high]){
high=mid-1;
}else{
low=mid+1;
}

}
return -1;
}
}
22 changes: 22 additions & 0 deletions FindPeakElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time Complexity:
// Space Complexity:
// Approach : Binary Search and move the pointers towards right or left
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-1]<nums[mid])&&(mid == n-1 || nums[mid+1]<nums[mid])){
return mid;
}
else if(mid>0 && nums[mid]<nums[mid-1]){
high=mid-1;
} else{
low=mid+1;
}
}
return -1;
}
}
53 changes: 53 additions & 0 deletions FirstAndLastElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity : logn + log n = O(logn)
// Space Complexity : O(1)
// Approach : Using Binary Search calculate first index of target which elements second half of arrya and using another binary search calculate second index which eliminates first half of array.
class Solution {
public int[] searchRange(int[] nums, int target) {
int n =nums.length;
if(n==0) return new int []{-1,-1};
int low = 0;
int high = nums.length -1;

int firstPosition = firstBinarySearch(nums,low,high,target);
if (firstPosition == -1){
return new int[]{-1,-1};
}

int lastPosition = secondBinarySearch(nums,firstPosition,high,target);
return new int []{firstPosition,lastPosition};
}
public int firstBinarySearch(int[] nums , int low, int high, int target){
while(low<=high){
int mid = low + (high-low)/2;
if(nums[mid]==target){
if(mid ==0 || nums[mid-1]<nums[mid]){
return mid;
} else {
high = mid-1;
}
} else if(nums[mid]<target){
low = mid+1;
} else{
high = mid-1;
}
}
return -1;
}
public int secondBinarySearch(int[] nums , int low, int high, int target){
while(low<=high){
int mid = low + (high-low)/2;
if(nums[mid]==target){
if(mid == nums.length-1 || nums[mid+1]>nums[mid]){
return mid;
} else {
low = mid+1;
}
} else if(nums[mid]>target){
high = mid-1;
} else{
low = mid+1;
}
}
return -1;
}
}