Question:

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 ______.

Note: Ignore syntax errors (if any) in the function.

Show Hint

Track the recursion by the value of length modulo 3: a "+1" is only added when length is 1 mod 3, and after that step the remainder can only cycle between 0 and 2, so it can never hit 1 again.
Updated On: Jul 22, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 1

Solution and Explanation

Step 1: Understand what one call does to the length.
Define
\[ \text{length} = end + 1 - start \]
which is the number of integers in the range from start to end. Each recursive call either returns 0 right away (the base case) or makes a fresh call with a smaller length, sometimes adding 1 to the final answer along the way. Specifically:
if \(\text{length} \bmod 3 = 0\), the next call has length reduced by 1, and adds nothing.
if \(\text{length} \bmod 3 = 1\), the next call has length reduced by 1, and adds 1 to the result.
if \(\text{length} \bmod 3 = 2\), the next call has length reduced by 2, and adds nothing.
The recursion stops and returns 0 as soon as length drops below 1 (or start or end goes negative).

Step 2: Track how the length's remainder mod 3 changes over the recursion.
Suppose the current length has remainder 0 when divided by 3. The next length is 1 less, so its remainder becomes 2.
Suppose the current length has remainder 2. The next length is 2 less, so its remainder becomes 0.
So once the remainder becomes 0 or 2, the chain of remainders just keeps bouncing between 0 and 2 forever after:
\[ 0 \to 2 \to 0 \to 2 \to \cdots \]
and neither of these two steps ever adds 1 to the answer.

Step 3: See what happens if the length starts with remainder 1.
If the very first length has remainder 1 mod 3, that first step adds 1 to the answer, and the next length has remainder 0 (since it drops by 1). From that point on, by Step 2, the remainder only ever bounces between 0 and 2, and no further 1's are ever added.

Step 4: Count the total number of times 1 can be added.
A "+1" is added only on a step where the current length's remainder mod 3 is exactly 1. From Step 2, once the remainder leaves the value 1, it can never return to 1 again (it only cycles between 0 and 2). So across the whole recursive chain, the remainder can equal 1 at most once, right at the very first call.
This means the function can add at most a single 1 in total, no matter how large start and end are chosen.

Step 5: Confirm this maximum is actually reachable.
Take start=0, end=1, so length=2 (remainder 2). This calls func(2,1), whose length is 0, so it returns 0 straight away, giving a total of 0 for this path.
Instead take start=0, end=0, so length=1 (remainder 1). This adds 1 and calls func(0,-1), whose end is negative, so it returns 0 immediately. Total returned is
\[ 1 + 0 = 1 \]
So a return value of 1 is indeed achievable, and by Step 4 it cannot go any higher.

Final Answer:
\[ \boxed{1} \]
Was this answer helpful?
0
0