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 c85e7c0

Browse filesBrowse files
authored
solved issue number dubesar#14 partially
added upper bound code
1 parent f5c42dd commit c85e7c0
Copy full SHA for c85e7c0

File tree

Expand file treeCollapse file tree

1 file changed

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

1 file changed

+50
-0
lines changed

‎UpperBoundInJava.java

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Just_Greater_Binary_Search {
2+
3+
// implementation of upper bound method as in C++ in Java using iterative binary search
4+
5+
static int findJustGreater(int inp[],int low ,int high,int x){
6+
7+
// doesn't handle equals to case , which is more often than not required
8+
9+
// iterative binary search
10+
11+
int mid ;
12+
int ans = -1 ;
13+
while(low<=high){
14+
mid = (low+high)/2 ;
15+
16+
if(inp[mid]<=x){
17+
18+
low = mid+1;
19+
20+
}else{
21+
22+
ans = mid;
23+
high = mid - 1;
24+
25+
}
26+
27+
}
28+
29+
return ans;
30+
31+
}
32+
33+
public static void main(String[] args) {
34+
35+
// already sorted input array
36+
int inp[] = {4,5,5,5,7,8,11,12,15,17};
37+
38+
/* i will contain the index ofjust greater element than
39+
11 in inp array (similar to upper bound in C++)
40+
*/
41+
42+
int i = findJustGreater(inp,0,inp.length-1,11);
43+
44+
// -1 implies no element greater than 11 in our inp array
45+
46+
if(i!=-1)
47+
System.out.println(i+" "+inp[i]);
48+
49+
}
50+
}

0 commit comments

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