equivalent of Lower_bound and Upper_bound statements in Java | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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???

31st Jul 2019, 6:32 PM
Mahdi Salmani
Mahdi Salmani - avatar
4 ответов
+ 1
attempt deleted
3rd Aug 2019, 12:41 PM
zemiak
+ 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
4th Aug 2019, 7:31 PM
Mahdi Salmani
Mahdi Salmani - avatar
+ 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.
5th Aug 2019, 10:42 PM
zemiak
0
Ok thank you very much :)
6th Aug 2019, 7:40 PM
Mahdi Salmani
Mahdi Salmani - avatar