Question:medium

Consider the following ANSI-C function.
int func(int start, int end){
int length=end+1-start;
if((length<1)||(start<0)||(end<0)){ return(0); }
if(length%3==0){
return(func(start+1, end));
} else if(length%3==1){
return(1+func(start, end-1));
} else {
return(func(start+2, end));
}
}
The maximum possible value that can be returned from this function is
____________. (answer in integer)
Note: Ignore syntax errors (if any) in the function.

Show Hint

Rewrite the recursion in terms of only n = end + 1 - start; only the branch where n mod 3 = 1 adds 1 to the result, and once n becomes a multiple of 3 it can never again hit that branch before reaching the base case, so at most a single +1 is ever added.
Updated On: Aug 3, 2026
Show Solution

Correct Answer: 1

Solution and Explanation

This problem asks for the largest value that a self-recursive C function can produce, and the trick is to notice that the plus-one contribution to the answer is extremely rare.

Setting up a single-variable model: let \(n = end - start + 1\) denote how many integers lie in the current range. The function short-circuits to 0 once \(n\) drops below 1 (or once start or end goes negative, which we can always avoid by starting with a big enough range). Looking at the three branches:

1. \(n \bmod 3 = 0\) moves to length \(n-1\) with no bonus added.
2. \(n \bmod 3 = 1\) moves to length \(n-1\) and adds exactly 1 to the eventual answer.
3. \(n \bmod 3 = 2\) moves to length \(n-2\) with no bonus added.

Key observation (invariant): suppose at some point the length becomes a multiple of 3, say \(n = 3k\). Then the very next step uses branch 1 (since \(3k \bmod 3 = 0\)), landing on length \(3k - 1\), which is congruent to 2 mod 3. Branch 3 then fires, landing on \(3k - 3\), again a multiple of 3. So from a multiple of 3, the sequence of lengths cycles strictly between residues 0 and 2 modulo 3 all the way down to 0, and it can never land back on a length congruent to 1 modulo 3. Since only a length congruent to 1 mod 3 ever triggers the bonus, once you pass through a multiple of 3 you can never earn another bonus.

Consequence: the only chance to earn a +1 is at the very first call, if the initial length \(n_0 = end - start + 1\) happens to be congruent to 1 mod 3. After that single possible bonus, the length becomes \(n_0 - 1\), a multiple of 3, and by the invariant above the rest of the recursion contributes nothing more before hitting the base case.

Verifying with the smallest case: take \(start = 0, end = 0\), so \(n_0 = 1\). Then \(func(0,0) = 1 + func(0,-1)\), and \(func(0,-1)\) has length \(0\), which is less than 1, so it returns 0 immediately. The total is \(1 + 0 = 1\).

Since no chain of recursive calls can ever earn more than a single +1 bonus, the value returned by this function is always either 0 or 1, for any valid start and end. The maximum possible value it can return is therefore:

\(\boxed{1}\)

Was this answer helpful?
0


Questions Asked in GATE CS exam