‹ Back to the paper
The following function int solve( ) is a part of some class. Assume ‘m’ and ‘n’ are positive…
The following function int solve( ) is a part of some class. Assume ‘m’ and ‘n’ are positive integers. Answer the questions given below with dry run / working.
int solve(int m, int n)
{
int k=1;
if(m<0)
return -k;
else if(m==0)
return m;
else
return k+(solve(m-n, n+2));
}(a)[2.0]
What will the function solve( ) return if:
(1) $m = 16, n = 1$
(2) $m = 9, n = 1$
(b)[1.0]
What is the function solve( ) performing apart from recursion?
Answer
Answer (a)
AIDry run for m=16, n=1:
solve(16,1) = 1 + solve(15,3)
solve(15,3) = 1 + solve(12,5)
solve(12,5) = 1 + solve(7,7)
solve(7,7) = 1 + solve(0,9)
solve(0,9) = 0 (since m==0)
Unwinding: solve(7,7)=1, solve(12,5)=2, solve(15,3)=3, solve(16,1)=4
solve(16,1) returns 4
Dry run for m=9, n=1:
solve(9,1) = 1 + solve(8,3)
solve(8,3) = 1 + solve(5,5)
solve(5,5) = 1 + solve(0,7)
solve(0,7) = 0
Unwinding: solve(5,5)=1, solve(8,3)=2, solve(9,1)=3
solve(9,1) returns 3
Answer (b)
AIApart from recursion, the function counts the number of recursive calls made (i.e. how many times n, then n+2, n+4, ... can be successively subtracted from m) before m reaches exactly 0.
From ISC 2025 Computer Science Paper 1, question 2(iii).