-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpperboundLowerbound.cpp
75 lines (65 loc) · 1.58 KB
/
UpperboundLowerbound.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
/*
>> Problem - cs->DS(8-1)1:17-2:0
>> Problem -
You are given an array of n positive integers. The next line will contain q queries, in each
query you will be given a type and an integer k.If the type is 1 then perform lower bound,
if the type is 2 then perform upper bound.If the element is not found in any query print -1
>> lower bound = if k present then return k otherwise return immediate greater element of k
>> upper bound = if k present or not return immediate greater element of k
Sample Input -
7
2 3 19 6 7 5 17
4
1 19
1 15
2 3
2 8
Sample Output -
6 -> 19
5 -> 17
2 -> 5
5 -> 17
>> Solution -
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0;i < n;i++)
cin >> v[i];
sort(v.begin(), v.end());
int q;
cin >> q;
while (q--)
{
int type, k;
cin >> type >> k;
if (type == 1)
{
auto it = lower_bound(v.begin(), v.end(), k);
if (it != v.end())
{
int idx = it - v.begin(), value = *it;
cout << idx << " -> " << value << '\n';
}
else
cout << -1 << '\n';
}
else
{
auto it = upper_bound(v.begin(), v.end(), k);
if (it != v.end())
{
int idx = it - v.begin(), value = *it;
cout << idx << " -> " << value << '\n';
}
else
cout << -1 << '\n';
}
}
return 0;
}