-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloor_ceil.cpp
66 lines (55 loc) · 1.34 KB
/
floor_ceil.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
//GFG - FInd floor and ceil value in an unsorted array
//TC = O(n) since array is not sorted
// SC = O(1)
#include<bits/stdc++.h>
using namespace std;
//first is more efficient
pair<int, int> floorAndCeil(int arr [], int n, int x){
int floor = -1;
int ceil = -1;
for(int i =0; i<n; i++){
//to find floor value
if(arr[i] <= x && (floor == -1 || arr[i] > floor)){
//it should be the greatest
floor = arr[i];
}
//find ceil value
if(arr[i] >= x && (ceil == -1 || arr[i] < ceil)) {
//it should be smallest such that -1 is not the smallest if there exists a ceil value
ceil = arr[i];
}
}
return {floor, ceil};
}
/*
pair<int, int> floorAndCeil(int arr [], int n, int x){
int floor = -1;
int ceil = -1;
for(int i =0; i<n; i++){
//to find floor value
if(arr[i] <= x){
//it should be the greatest
floor = max(floor, arr[i]);
}
//find ceil value
if(arr[i] >= x) {
//it should be smallest such that -1 is not the smallest if there exists a ceil value
ceil = (ceil == -1)? ceil = arr[i] : min(ceil, arr[i]);
}
}
return {floor, ceil};
}
*/
int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i<n; i++){
cin >> arr[i];
}
int x;
cin>>x;
auto ans = floorAndCeil(arr, n, x);
cout<< ans.first <<" " << ans.second;
return 0;
}