Arrays-Binary Search

Take as input N, the size of array. Take N more inputs and store that in an array. Take as input a number M. Write a function which returns the index on which M is found in the array, in case M is not found -1 is returned. Print the value returned.You can assume that the array is sorted, but you’ve to optimize the finding process. For an array of size 1024, you can make 10 comparisons at maximum.

1.It reads a number N.
2.Take Another N numbers as input in Ascending Order and store them in an Array.
3.Take Another number M as input and find that number in Array.
4.If the number M is found, index of M is returned else -1 is returned.Print the number returned.

THIS IS MY CODE PLEASE TELL ME WHAT IS THE MISTAKE IN CODE AND PLEASE CORRECT IT(AND MENTION IN COMMENTS)

Hello @dhruvk3k please wait.

@dhruvk3k correted code:

bhai code mein kya galti thi??

@dhruvk3k you have to compare with the a[mid] but you were doing it with mid.
we have to compare with the element at the mid and not the index.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.