ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int bs(const int *a, int lo, int hi, int k) {
    if (lo > hi) return -1;
    int m = lo + (hi - lo) / 2;
    if (a[m] == k) return m;
    return a[m] < k ? bs(a, m + 1, hi, k) : bs(a, lo, m - 1, k);
}
int main(void) {
    int a[] = {1, 3, 5, 7, 9, 11};
    printf("%d %d", bs(a, 0, 5, 9), bs(a, 0, 5, 4));
}
  1. A. 4 -1
  2. B. 3 -1
  3. C. 5 -1
  4. D. 4 0

Correct answer: A. 4 -1

9 sits at index 4, and the value 4 is absent, so the search returns -1 once lo exceeds hi. The recursive halving visits only log n positions.

More C Programming questions

Exams that ask this topic