Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Search in C++ with array

Here is an array with exactly 15 elements:

1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

Suppose that we are doing a binary search for an element. Indicate any elements that will be found by examining two or fewer numbers from the array.

What I've got: as we are doing binary search, so the number found by only one comparison will be 7th element = 7. For two comparison, this leads to second division of array. That is, number found can be either 3 or 11.

Am I right or not?

like image 776
user2569893 Avatar asked Nov 03 '22 17:11

user2569893


1 Answers

You are almost right, the first number is not seven but eight.

The others 2 will then be 4 and 12.

The correct answer would be 4, 8, 12

like image 78
vals Avatar answered Nov 11 '22 20:11

vals