Data Structures & AlgorithmsHardGATE CS · competitive programming

rec(n) returns at once if n ≤ 1; otherwise it calls rec(n/2) twice with integer division. How many calls are made for n = 16, counting the top call?

  1. A. 16
  2. B. 32
  3. C. 31
  4. D. 15

Correct answer: C. 31

C(1) = 1 and C(n) = 1 + 2·C(n/2), so C(2)=3, C(4)=7, C(8)=15, C(16)=31 = 2n − 1. Counting only the leaves gives 16 and misses the internal calls.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic