-
Notifications
You must be signed in to change notification settings - Fork 0
/
Left_Most_And_Right_Most_Of_Indexs.cpp
86 lines (78 loc) · 1.78 KB
/
Left_Most_And_Right_Most_Of_Indexs.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
int upper_bound(vector<long long> arr, long long x)
{
int res = 0;
int low = 0;
int high = arr.size()-1;
while(low <= high)
{
int mid = (low + high) / 2;
if(arr[mid] <= x)
{
res = mid;
low = mid+1;
}
else high = mid-1;
}
return arr[res] == x ? res : -1;
}
int lower_bound(vector<long long> arr, long long x)
{
int res = 0;
int low = 0;
int high = arr.size()-1;
while(low <= high)
{
int mid = (low + high) / 2;
if(arr[mid] < x)
{
low = mid+1;
}
else
{
res = mid;
high = mid-1;
}
}
return arr[res] == x ? res : -1;
}
pair<long,long> indexes(vector<long long> v, long long x)
{
int i = upper_bound(v, x);
int j = lower_bound(v, x);
return {j, i};
}
};
//{ Driver Code Starts.
int main()
{
long long t;
cin>>t;
while(t--)
{
long long n, k;
vector<long long>v;
cin>>n;
for(long long i=0;i<n;i++)
{
cin>>k;
v.push_back(k);
}
long long x;
cin>>x;
Solution obj;
pair<long,long> pair = obj.indexes(v, x);
if(pair.first==pair.second and pair.first==-1)
cout<< -1 <<endl;
else
cout<<pair.first<<" "<<pair.second<<endl;
}
return 0;
}
// } Driver Code Ends