-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18_2.java
More file actions
46 lines (35 loc) · 970 Bytes
/
day18_2.java
File metadata and controls
46 lines (35 loc) · 970 Bytes
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
//Given a sorted array arr[] of size N without duplicates, and given a value x.
//Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.
//Find the index of K(0-based indexing).
class Solution{
// Function to find floor of x
// arr: input array
// n is the size of array
static int findFloor(long arr[], int n, long x)
{
long max = -1;
int low =0;
int high =n-1;
while(low<=high)
{
int mid = (low+high)/2;
if(arr[mid]<x)
{
low =mid+1;
}
else if(arr[mid]>x)
{
high =mid-1;
}
else
{
return mid;
}
}
if(high<0 || low>n)
{
return -1;
}
return low-1;
}
}