+ 3

will binary search work when length of array is odd.

to me it seems that it will never reach the last index, plz let me know how it search the last index. https://code.sololearn.com/c7gsuiSEOitM/?ref=app

25th Nov 2017, 2:06 PM
shobhit
shobhit - avatar
1 Answer
+ 2
unless I'm mistaken, that code doesn't look right. try this. (using c++) int binarySearch(const int arr[], int target) { int low = 0; int high = 2; int mid; while(low <= high) { mid = (low + high) / 2; if(target == arr[mid])} return mid; } else if(target > arr[mid]) { low = mid + 1; } else { high = mid - 1; } } return -1; }
26th Nov 2017, 3:08 AM
Gilbert Keys
Gilbert Keys - avatar