Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement

What does this loop-counting code print? (Python)

What does this loop-counting code print? (Python)
n, c = 16, 0
i = 1
while i < n:
    j = i
    while j < n:
        c += 1
        j *= 2
    i += 1
print(c)
  1. A. 28
  2. B. 26
  3. C. 25
  4. D. 64

Correct answer: B. 26

For each i the inner loop runs about log₂(n/i) + 1 times; summing over i gives Θ(n). Exact count: 26.

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

Exams that ask this topic