Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1b8c184

Browse filesBrowse files
authored
Solved Issue dubesar#14 completely
Added Lower bound implementation in Java.
1 parent c85e7c0 commit 1b8c184
Copy full SHA for 1b8c184

File tree

Expand file treeCollapse file tree

1 file changed

+47
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+47
-0
lines changed

‎LowerBoundInJava.java

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Just_Smaller_Binary_Search {
2+
3+
// implementation of lower bound as in C++ in Java
4+
5+
static int findJustSmaller(int inp[],int low,int high,int x){
6+
7+
// iterative binary search used in implementation
8+
9+
int ans = -1;
10+
11+
while(low<=high){
12+
13+
int mid = (low+high)/2;
14+
15+
if(inp[mid]>=x){
16+
17+
high = mid-1;
18+
19+
}else{
20+
21+
ans = mid;
22+
low = mid+1;
23+
24+
}
25+
}
26+
27+
return ans;
28+
29+
}
30+
31+
public static void main(String[] args) {
32+
33+
// array already sorted
34+
35+
int inp[] = {4,5,5,5,7,8,11,12,15,17};
36+
37+
/* index i will contain the index of element in inp
38+
array which is just smaller than 7*/
39+
40+
int i = findJustSmaller(inp,0,inp.length-1,7);
41+
42+
// -1 implies no such element exsists in inp array
43+
44+
if(i!=-1)
45+
System.out.println(i+" "+inp[i]);
46+
}
47+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.