+ 4

Why this code is not working?

i think it's due to stackoverflow but i m not doing any recursion or not calling any function again and again. https://code.sololearn.com/cR2ATN4dxVgZ/#cpp

28th Nov 2017, 12:21 PM
shobhit
shobhit - avatar
3 Answers
+ 2
The loop never ends, thats why. The condition low<=high never becomes false when no match is found. As in the end, both become mid...
28th Nov 2017, 1:25 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
In your code, when you meet the conditions : arr[mid] > target or arr[mid] < target, You set high to mid-1 and low to mid +1 respectively, not just mid. Otherwise, the program gets slow. Change the assignments, and you're done.
28th Nov 2017, 12:39 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
but why speed becomes slow?
28th Nov 2017, 12:44 PM
shobhit
shobhit - avatar