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

Let n denote a positive integer. A function f is defined as
f(n) = 0, if n = 1
f(n) = f(⌊n/2⌋) + 1, if n > 1
What is f(25), and what does this function find?

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

Show answer & explanation

Correct answer: A. 4, ⌊log2 n⌋

Explanation

f(25) = f(12)+1 = f(6)+2 = f(3)+3 = f(1)+4 = 4. Halving until 1 counts ⌊log2 n⌋ steps (⌊log2 25⌋ = 4).

More Data Structures and Algorithms questions