Can anyone help me with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me with this code?

The findElement function was supposed to return 4 but it's returning -1. https://code.sololearn.com/cbzZQA8QZXIu/?ref=app

15th Oct 2022, 12:35 PM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
4 Answers
+ 1
For binary search, the elements must be in sorted order..!
15th Oct 2022, 12:54 PM
Jayakrishna 🇮🇳
+ 1
I watched a YouTube explanation of this problem and it says this uses binarysearch cuz a part of the array is sorted so it does binary search on that part!Jayakrishna🇮🇳
15th Oct 2022, 1:18 PM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 1
Ok. index 4<4 is stoping loop so < instead of <= in while(start <= end) solve issue. but it may be only for this case.
15th Oct 2022, 1:24 PM
Jayakrishna 🇮🇳
+ 1
//while (start < end) { while (start <= end) { I checked only this array example but it can search all elements
17th Oct 2022, 8:01 PM
zemiak