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

Complete Binary-Search-2 #1925

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
58 changes: 58 additions & 0 deletions Find_First_and_last_element_of_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// for this particular problem we can perfrom the binary search for finding the first element
// and we if find the first element we compare it with its previous element if it is not the
// first index we can perfrom continue the binary search on left half
// once we have frist index we can perfrom the binary search on the right half starting from first index
// if we find the target we compare it with its next index if it is not the target we got our last index


class Solution {
int BinarySearchFirst(int start, int end, int target,vector<int>& nums)
{
while(start<=end)
{
int mid = start + (end- start)/2;
if(nums[mid]==target)
{
if(mid == 0 || nums[mid-1]!=target)
return mid;
else
end = mid -1;
}
else if( target<=nums[mid])
end = mid -1;
else
start = mid +1;
}
return -1;
}
int BinarySearchSecond(int start, int end, int target,vector<int>& nums)
{
while(start<=end)
{
int mid = start + (end- start)/2;
if(nums[mid]==target)
{
if(mid == end ||nums[mid+1]!=target)
return mid;
else
start = mid +1;
}
else if(target <= nums[mid])
end = mid -1;
else
start = mid +1;
}
return -1;
}
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.size() == 0) return {-1,-1};
int first = BinarySearchFirst(0, nums.size()-1,target,nums);
if(first == -1)
return {-1,-1};
int second = BinarySearchSecond(first,nums.size()-1,target,nums);
if(second == -1)
return {first ,first};
return{first,second};
}
};
19 changes: 19 additions & 0 deletions Find_peak_element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int start = 0;
int end = nums.size()-1;

while(start <= end)
{
int mid = start + (end-start)/2;
if((mid == 0 || nums[mid] > nums[mid-1]) && (mid == nums.size()-1||nums[mid] > nums[mid+1]))
return mid;
else if(mid > 0 && nums[mid-1] > nums[mid])
end = mid-1;
else
start = mid+1;
}
return -1;
}
};
27 changes: 27 additions & 0 deletions find_the_minimum_rotated_sorted_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int findMin(vector<int>& nums) {
int start = 0;
int end = nums.size() -1;
//base case
if(nums.size()==1)
return nums[0];
if(nums[start] < nums[end])
return nums[start];

while(start <= end)
{
int mid = start + (end-start)/2;
if(nums[mid] > nums[mid +1])
return nums[mid+1];
if(nums[mid- 1] > nums[mid] )
return nums[mid];
else if (nums[mid] > nums[start])
start = mid +1;
else
end = mid -1;
}
return -1;
}
};