Data Structures & AlgorithmsHardProduct-company MCQ pattern

What are the time complexities of versions P and Q?

What are the time complexities of versions P and Q?
int P(int n) { if (n <= 1) return 1; return P(n/2) + P(n/2) + n; }
int Q(int n) { if (n <= 1) return 1; int x = Q(n/2); return x + x + n; }
  1. A. P: O(log n), Q: O(log n)
  2. B. P: O(n log n), Q: O(log n)
  3. C. P: O(n), Q: O(log n)
  4. D. P: O(n), Q: O(n)

Correct answer: C. P: O(n), Q: O(log n)

P makes two recursive calls: T(n) = 2T(n/2) + O(1) = O(n) (2047 calls for n = 1024). Q calls itself once and reuses the result: T(n) = T(n/2) + O(1) = O(log n) (11 calls). Halving the argument alone does not make P logarithmic.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic