-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34.cpp
More file actions
36 lines (32 loc) · 1.06 KB
/
34.cpp
File metadata and controls
36 lines (32 loc) · 1.06 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
// Problem : 34. Find First and Last Position of Element in Sorted Array
// Link : https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> searchRange(vector<int> &nums, int target) {
vector<int> v(2, -1); // array of 2 -1's
if (!binary_search(nums.begin(), nums.end(), target)) //binary search for target
return v;
v.clear();
v.push_back(lower_bound(nums.begin(), nums.end(), target) - nums.begin()); // find lower bound for target
v.push_back((upper_bound(nums.begin(), nums.end(), target) - nums.begin()) - 1); // find upper bound of target
return v;
}
};
int main() {
vector<int> data;
data.push_back(5);
data.push_back(7);
data.push_back(7);
data.push_back(8);
data.push_back(8);
data.push_back(10);
Solution ob;
vector<int> solution = ob.searchRange(data, 8);
for (auto i: solution) {
cout << i << " ";
}
}