2,188 views
4 4 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 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
Position:
Show:

Related questions

0 0 votes
1 1 answer
389
389 views
Shubham Sharma 2 asked Sep 9, 2025
389 views
Consider the function in $C$ code:Cal(a,b) { if (b!=1) { if (a!=1) { printf("*"); Cal(a/2, b); } else { b=b-1; Cal(10,b); } }}How many times $*$ is going to be printed, i...
1 1 vote
0 0 answers
797
797 views
soujanyareddy13 asked Dec 7, 2021
797 views
What is the value of $f(4)$ using the following $C$ code:int f(int k){ if(k<3) return k; else return f(k-1) * f(k-2) + f(k-3); }$5$$6$$7$$8$
0 0 votes
1 1 answer
349
349 views
vishnusainune asked Dec 30, 2025
349 views
A recurrence arises in the problem: Number of ways to tile a 3×n rectangle with 2×1 dominoes (dominoes can be placed vertically or horizontally). Let T(n)​ be this number...