-
Notifications
You must be signed in to change notification settings - Fork 994
Expand file tree
/
Copy pathfirstAndLastPos.cpp
More file actions
77 lines (59 loc) · 1.6 KB
/
Copy pathfirstAndLastPos.cpp
File metadata and controls
77 lines (59 loc) · 1.6 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 7 . link : [ https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array ]
#include<bits/stdc++.h>
using namespace std;
int binarySearch(vector<int>& nums, int l,int r, int target){
if(l>r){
return -1;
}
int mid = l+(r-l)/2;
if(nums[mid] == target)
return mid;
else if(nums[mid] > target)
return binarySearch(nums,l,mid-1,target);
else
return binarySearch(nums,mid+1,r,target);
}
vector<int> searchRange(vector<int>& nums, int target) {
int pos = binarySearch(nums,0,nums.size()-1, target);
if(pos < 0)
return {-1,-1};
int left = pos;
int right = pos;
while(left > 0){
if(nums[left-1] != target)
break;
left--;
}
while(right < nums.size()-1){
if(nums[right+1] != target)
break;
right++;
}
return {left,right};
}
/*
1
9
5 7 7 8 8 8 9 10 11
8
3 5
*/
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test;
cin >> test;
while(test--){
int n;
cin >> n;
vector<int> arr(n);
for(int i=0; i<n; i++)
cin >> arr[i];
int target ; cin >> target;
vector<int> index = searchRange(arr , target);
cout << index[0] << " " << index[1] << endl;;
}
return 0;
}