Skip to content

Commit 65d9e93

Browse files
committed
FindFloor
1 parent cfb3ada commit 65d9e93

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Searching/FindFloor.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package SummerTrainingGFG.Searching;
2+
/**
3+
* @author Vishal Singh */
4+
public class FindFloor {
5+
static int findFloor(long[] arr,int n,long element){
6+
return find(arr,0,n-1,element);
7+
}
8+
static int find(long[] arr,int l,int h,long element){
9+
if (l>h){
10+
return -1;
11+
}
12+
int mid = l + (h-l)/2;
13+
if (arr[mid] == element){
14+
return mid;
15+
}
16+
if (mid > 0 && arr[mid-1] <= element && arr[mid] > element ){
17+
return mid-1;
18+
}
19+
if (arr[mid] > element){
20+
return find(arr,l,mid-1,element);
21+
}
22+
if (arr[mid] < element){
23+
return find(arr,mid+1,h,element);
24+
}
25+
return -1;
26+
}
27+
public static void main(String[] args) {
28+
long[] arr = {1, 2, 8, 10, 11, 12, 19};
29+
30+
long[] testCase = {0,1,2,10,19,13,8};
31+
32+
for (long l : testCase) {
33+
int index = findFloor(arr, arr.length, l);
34+
if (index == -1) {
35+
System.out.println("No such element");
36+
continue;
37+
}
38+
System.out.println("Floor of "+l+" at "+"Index: " + index + " Element: " + arr[index]);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)