All mock tests are FREE till 31 December — register to save your progress
Unit 7 · Data Structures and Algorithms Paper 2 CS · November 2020

With preconditions a > 1 and a < b (no overflow): int x = 0; int p = 1; while (p < b) { p = p * a; x = x + 1; } When the loop ends, x in terms of a and b is

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. ⌈log_a b⌉ (ceiling)

Explanation

p takes the values a, a², … and stops at the first power with aˣ ≥ b, so x = ⌈log_a b⌉.

More Data Structures and Algorithms questions