-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCeilingAndFloorInUnsortedArray.cpp
59 lines (40 loc) · 1.19 KB
/
CeilingAndFloorInUnsortedArray.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
#include<iostream>
using namespace std;
/*Program to find Ceiling and floor in an unsorted array
Algorithm-
The idea is to traverse array and keep track of two distances with respect to x.
1) Minimum distance of element greater than or equal to x.
2) Minimum distance of element smaller than or equal to x.
Finally print elements with minimum distances.
*/
int f=0 ,c=0;
void floorAndCeil(int *arr,int key, int n)
{
// Distances of current floor and ceiling
int fDist = INT_MAX, cDist = INT_MAX;
for (int i=0; i<n; i++)
{
// If current element is closer than
// previous ceiling.
if (arr[i] >= key && cDist > (arr[i] - key))
{
c = arr[i];
cDist = arr[i] - key;
}
// If current element is closer than
// previous floor.
if (arr[i] <= key && fDist > (key - arr[i]))
{
f = arr[i];
fDist = key - arr[i];
}
}
}//Time complexity = O(n) and space complexity = O(1)
int main()
{
int arr[ ] = {9,1,23,2,10,8};
int size = sizeof(arr)/sizeof(arr[0]);
floorAndCeil(arr,20,size-1);
cout<<"Floor is: "<<f<<endl;
cout<<"Ceiling is: "<<c<<endl;
}