-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_for_range.cpp
50 lines (48 loc) · 1.05 KB
/
search_for_range.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
// Problem link - https://www.interviewbit.com/problems/search-for-a-range/
#include <bits/stdc++.h>
using namespace std;
vector<int> searchange(vector<int> A, int B) {
int n = A.size();
int low = 0;
int high = n-1;
int mid = (low+high)/2;
vector<int>v(2);
v[0] = -1; v[1] = -1;
while (low<=high)
{
mid = (low+high)/2;
if (A[mid]==B)
{
v[0] = mid;
high = mid-1;
}
else if(A[mid]<B) low=mid+1;
else high=mid-1;
}
if (v[0]== -1)
{
return v;
}
low = v[0];
high = n-1;
while (low<=high)
{
mid = (low+high)/2;
if(A[mid==B])
{
v[1] = mid;
low = mid+1;
}
else if(A[mid]<B) low=mid+1;
else high=mid-1;
}
return v;
}
int main(){
vector<int> A = {1, 1, 1, 1, 1, 1, 1, 1 , 10, 10, 10, 10, 10, 10, 10, 10 }; // This has to be assumed
vector<int> B = searchange(A, 10);
for (int i = 0; i < B.size(); i++)
{
cout<<B[i]<<" ";
}
}