How does Binary Search perform when there are multiple occurrences of the search key in the data?

  • It returns an error due to ambiguity.
  • It returns the index of the first occurrence.
  • It returns the index of the last occurrence.
  • It returns the index of the middle occurrence.
Binary Search is designed to find the index of the last occurrence of the search key when there are multiple occurrences. This is because, in binary search, if a match is found, the algorithm continues searching in the right subarray to ensure it returns the last occurrence. This behavior is efficient for tasks like counting occurrences. The other options do not accurately describe the behavior of Binary Search in this context.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *