-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34. Find First and Last Position of Element in Sorted Array
50 lines (47 loc) · 1.8 KB
/
34. Find First and Last Position of Element in Sorted Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//🎯DAY 17 PROBLEM 1
//The idea is simple to binary search for the element in the array and then maintain two pointers, one start and one end and keep on incrementing and decrementing untill we get
//the same element.
class Solution {
int binarySearch(int l, int r, vector<int>&nums, int target)
{
int mid=l+ (r-l)/2;
if(l<=r)
{
if(nums[mid]==target)
return mid;
if(nums[mid]>target)
return binarySearch(0, mid-1, nums, target);
if(nums[mid]<target)
return binarySearch(mid+1, r, nums, target);
}
return -1;
}
public:
vector<int> searchRange(vector<int>& nums, int target) {
int pos=binarySearch(0,nums.size()-1,nums,target);
int start=pos,end=pos;
if(nums.empty())return {-1,-1};
if(pos ==-1)
return {-1,-1};
else
{
//😨😨Don't forget to check nums[pos+1] not nums[pos] ==target as when we check for pos+1, then we make end=pos+1 in the while loop.
while(pos<nums.size()-1&&nums[pos+1]==target)
{
end++;
pos++;
}
pos=start;
//📌REMEMBER📌
//😨😨Similarly,Don't forget to check nums[pos-1] not nums[pos] ==target as when we check for pos-1, then we make start=pos-1 in the while loop.If we check for pos,
//and then make start-- i.e. initially start was pos, now it becomes pos-1, it may be possible that nums[pos-1]!=target and we return that as we have earlier checked
//for nums[pos].
while(pos>0&&nums[pos-1]==target)
{
start--;
pos--;
}
}
return {start,end};
}
};