GATE CS 2023 · Asymptotic Analysis & Recurrences

MSQ · 2 marks · Problem PROG-DIVC-0074

Consider functions Function 1 and Function 2 expressed in pseudocode as follows:Function 1:
while n > 1 do
    for i = 1 to n do
        x = x + 1;
    end for
    n = floor(n/2);
end while
Function 2:
for i = 1 to 100 * n do
    x = x + 1;
end for
Let \(f_1(n)\) and \(f_2(n)\) denote the number of times the statement "x = x + 1" is executed in Function 1 and Function 2, respectively.Which of the following statements is/are TRUE?
  1. A
    \(f_1(n)\in \Theta (f_2(n))\)
  2. B
    \(f_1(n)\in o (f_2(n))\)
  3. C
    \(f_1(n)\in \omega (f_2(n))\)
  4. D
    \(f_1(n)\in O  (n)\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{f_1(n)=n+\left\lfloor\tfrac n2\right\rfloor+\left\lfloor\tfrac n4\right\rfloor+\cdots<2n,\qquad f_2(n)=100n}\]Function 1 runs the inner loop for \(n, n/2, n/4,\ldots\) down to 2, so \(n\le f_1(n)<2n\): \(f_1(n)=\Theta(n)\). Function 2 executes the statement exactly \(100n\) times: \(f_2(n)=\Theta(n)\).(A) \(f_1\in\Theta(f_2)\): both are \(\Theta(n)\). True.(B) \(f_1\in o(f_2)\): \(f_1/f_2\to\) a constant between \(\tfrac1{100}\) and \(\tfrac1{50}\), not 0. False.(C) \(f_1\in\omega(f_2)\): false for the same reason.(D) \(f_1\in O(n)\): \(f_1<2n\). True.Answer: A, D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com