diff --git a/Find_First_and_last_element_of_Array.cpp b/Find_First_and_last_element_of_Array.cpp new file mode 100644 index 00000000..1422b061 --- /dev/null +++ b/Find_First_and_last_element_of_Array.cpp @@ -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& 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& 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 searchRange(vector& 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}; + } +}; \ No newline at end of file diff --git a/Find_peak_element.cpp b/Find_peak_element.cpp new file mode 100644 index 00000000..340a2ffe --- /dev/null +++ b/Find_peak_element.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int findPeakElement(vector& 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; + } +}; \ No newline at end of file diff --git a/find_the_minimum_rotated_sorted_array.cpp b/find_the_minimum_rotated_sorted_array.cpp new file mode 100644 index 00000000..31959bbb --- /dev/null +++ b/find_the_minimum_rotated_sorted_array.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int findMin(vector& 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; + } +}; +