The document discusses a pseudocode program that calls functions f(n) and g(n). It asks (a) to express the return value of g(n) in terms of n, (b) the time complexity of g(n), and (c) a recurrence relation for the number of lines printed by f(n). It then shows the work to solve the recurrence relation exactly for part (d).
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
12 views
Algo
The document discusses a pseudocode program that calls functions f(n) and g(n). It asks (a) to express the return value of g(n) in terms of n, (b) the time complexity of g(n), and (c) a recurrence relation for the number of lines printed by f(n). It then shows the work to solve the recurrence relation exactly for part (d).
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
This question is to be attempted individually.
Consider the following pseudocode: int f(int n) { if
(n>1) { f(n/2); f(n/2); for(int i = 1; i <= g(n); i++) { println("Hello world!"); } f(n/2); } } int g(int n) { int sum = 0; for(int i = 1; i <= n; i++) sum += i; return sum; } Here println() is a function that prints a line of text. Assume n is a power of 2. (a) Express the return value of g(n) in terms of n. [5 marks] (b) What is the time complexity of g(n), in terms of n and in big-O? [5 marks] (c) Let T(n) be the number of lines printed by f(n). Write down a recurrence formula for T(n), including the base case. [5 marks] (d) Solve the recurrence in (c), showing your working steps. For full credit give the exact answer (not big-O) and do not use Master Theorem. [20 marks]
(a) in g(n) we are doing summation like
1+2+3+4...till n = n*(n+1)/2
hence return value is n*(n+1)/2
(b) since we are running the loop for i to n hence time complexiyt is O(n)
(c) here the recurrence formula is
T(n) = 2*T(n/2) + n*(n+1)/2 // as g(n) returns n*(n+1)/2