1,110 views
5 votes
5 votes

Consider the following function:

void f(int a)
{
        if(a <= 0)
                return ;
        else{
                printf("%d ",a);
                f(a-2);
                printf("%d ",a);
                f(a-3);
        }
}

The sum of all values printed by f(6) is _____ .

1 Answer

Best answer
2 votes
2 votes
f(0) does not print anything.

f(1) prints 1 and 1.

f(n) prints n, f(n-2), again n and then f(n-3).

So, we can have the recurrence relation for the sum of values being printed as

$P(n) = 2n + P(n-2) + P(n-3), P(n\leq0) = 0.$

$P(1) = 2, P(2) = 4, P(3) = 8, P(4) = 14$

$P(6) = 2\times 6 + P(4) + P(3) \\=12 + 14 + 8 = 34.$
selected by

Related questions

4 votes
4 votes
0 answers
1
Aakanchha asked Jan 8, 2018
797 views
What is the highest upper bound time complexity for the following recurrence equation: $T(n)=4T\lef...
9 votes
9 votes
1 answer
2
anoop yadav 2 asked Jan 8, 2018
13,316 views
How to solve above recurrence relation (With substitution method)??
2 votes
2 votes
0 answers
3
Sachi Saxena 11 asked Jul 6, 2017
198 views
$T(n) = 1/n\sum T(I) + 1.$Sum ranges fromI=1 to i=n-1
1 votes
1 votes
0 answers
4