forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowerBoundInJava.java
More file actions
47 lines (27 loc) · 1.05 KB
/
LowerBoundInJava.java
File metadata and controls
47 lines (27 loc) · 1.05 KB
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
public class Just_Smaller_Binary_Search {
// implementation of lower bound as in C++ in Java
static int findJustSmaller(int inp[],int low,int high,int x){
// iterative binary search used in implementation
int ans = -1;
while(low<=high){
int mid = (low+high)/2;
if(inp[mid]>=x){
high = mid-1;
}else{
ans = mid;
low = mid+1;
}
}
return ans;
}
public static void main(String[] args) {
// array already sorted
int inp[] = {4,5,5,5,7,8,11,12,15,17};
/* index i will contain the index of element in inp
array which is just smaller than 7*/
int i = findJustSmaller(inp,0,inp.length-1,7);
// -1 implies no such element exsists in inp array
if(i!=-1)
System.out.println(i+" "+inp[i]);
}
}