From f553455c7f6fff530dfd036dfa4fce5aab12755e Mon Sep 17 00:00:00 2001 From: Pratiksha Kapse Date: Tue, 12 May 2026 16:32:21 -0700 Subject: [PATCH] Completed BS2 --- problem1.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ problem2.cpp | 22 ++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 problem1.cpp create mode 100644 problem2.cpp diff --git a/problem1.cpp b/problem1.cpp new file mode 100644 index 00000000..a024248d --- /dev/null +++ b/problem1.cpp @@ -0,0 +1,59 @@ +class Solution { +public: + vector searchRange(vector& nums, int target) { + if(nums.size()==0) + return {-1,-1}; + + int n=nums.size()-1; + int low=0, high=n; + + int index1=BS1( target, nums); + if(index1==-1) + return {-1,-1}; + + int index2=BS2( target, nums); + + return {index1, index2}; + } + + int BS1(int target,vector& nums) + { + int low=0, high=nums.size()-1; + while(low<=high) + { + int mid=low+(high-low)/2; + if(nums[mid]==target) + { + if(mid==low || nums[mid]!=nums[mid-1]) + return mid; + high=mid-1; + } + else if(nums[mid]& nums) + { + int low=0, high=nums.size()-1; + while(low<=high) + { + int mid=low+(high-low)/2; + if(nums[mid]==target) + { + if((mid==high)|| nums[mid]!=nums[mid+1]) + return mid; + low=mid+1; + } + + else if(nums[mid]& nums) { + if(nums.size()==0) + return -1; + int low=0, high=nums.size()-1; + + while(low<=high) + { + if(nums[low]<=nums[high]) + return nums[low]; + int mid=low+(high-low)/2; + if((mid==low || nums[mid]