ProgrammingHard

A program must answer 10^6 queries 'is x present in the set S?' where S has 10^6 values fixed in advance. Which preprocessing and query cost is correct and fast enough?

  1. A. Sort S once in O(n log n), then each query is a binary search in O(log n), about 2 x 10^7 steps total
  2. B. Scan S for each query, about 10^12 steps
  3. C. Sort S before each query, about 10^13 steps
  4. D. Compare x with all pairs in S, about 10^12 steps

Correct answer: A. Sort S once in O(n log n), then each query is a binary search in O(log n), about 2 x 10^7 steps total

Preprocessing is O(n log n) once and queries cost O(log n), so the total stays near 2 x 10^7. Scanning S each time is quadratic.

More C Programming questions

Exams that ask this topic