ProgrammingModerate

Binary search on the answer: a monotone check runs in O(n) and the answer lies in [1, 10^9]. What is the total complexity?

  1. A. O(n^2), since each check is a nested loop
  2. B. O(10^9) because the answer range is scanned linearly
  3. C. O(n log n), since the array is sorted first
  4. D. O(n log(10^9)), about 30 checks of O(n) each

Correct answer: D. O(n log(10^9)), about 30 checks of O(n) each

log2(10^9) is about 30, so the search performs about 30 checks, each linear. The answer range is never scanned one value at a time.

More C Programming questions

Exams that ask this topic