binary search for array with EVEN number of elements... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

binary search for array with EVEN number of elements...

Hi there In the binary search algorithm, as you know, we need to select the middle element of the array. It’s obvious for an array with odd number of elements. for example in arr[5], the middle element would be arr[2] (- - * - - ) . now I wanna ask what happens when the number of elements is EVEN ? arr[4] for instance !? (- - - -) Already thanks for replies🙏🏻

27th Feb 2021, 8:12 PM
Ali Hosseinzadeh
Ali Hosseinzadeh - avatar
1 Answer
+ 3
you have to arbitrary choose one of the two middle elements... usually we use the result of length/2 casted to integer (floor/integer division)...
27th Feb 2021, 8:14 PM
visph
visph - avatar