+ 2
equivalent of Lower_bound and Upper_bound statements in Java
What is the equivalent of Lower_bound and Upper_bound statements in Java which used in Cpp???
4 Respuestas
+ 1
attempt deleted
+ 1
that code is wrong
for example in this array
3 4 5 5 5 6
when you binarysearch 5
this method returns 3
and in this one
3 4 5 5 5 5 5 6 it returns 4
so you cant obtain Lower_bound and Upper_bound with the binary search
+ 1
I get the different result from your data, but the result for second sample was incorrect.
The binary search API includes:
"If the array contains multiple elements with the specified value, there is no guarantee which one will be found.."
So you can use binarySearch() to find a part of numbers with your value, but then reversed search with 'for loop' the begin of this part (lower bound), eg 4,4,5,5,<-(5),5,5,6,6
or write your own method (generic or not) as better variation of binary search , I've seen several on the web.
0
Ok thank you very much :)