-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirst_and_last_index.cpp
54 lines (49 loc) · 1.1 KB
/
first_and_last_index.cpp
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
51
52
53
54
#include<bits/stdc++.h>
using namespace std;
vector<int> searchRange(vector<int>& nums, int target) {
//find first occurence
int low = 0, high = nums.size()-1;
int mid,first =-1,last=-1;
vector <int> res;
if(nums.size()<1)
{
res.push_back(first);
res.push_back(last);
return res;
}
while(low<=high)
{
mid = (low+high)/2;
if(nums[mid] >= target)
high = mid-1;
else
low = mid+1;
if(nums[mid]==target)
first = mid;
}
//find last occurence
low = 0, high = nums.size()-1;
while(low<=high)
{
mid = (low+high)/2;
if(nums[mid] <= target)
low = mid+1;
else
high = mid-1;
if(nums[mid]==target)
last = mid;
}
// cout<<first<<endl;
// cout<<last<<endl;
res.push_back(first);
res.push_back(last);
return res;
}
int main()
{
vector <int> A{5,7,7,8,8,10};
vector <int> ans = searchRange(A,8);
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
return 0;
}