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
____________. (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: Jul 7, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 1

Solution and Explanation

We need the maximum value the recursive function func(start, end) can ever return.

Step 1: Reduce the problem to a single variable. Let \(n = end + 1 - start\) be the length of the range. The function returns 0 immediately whenever \(n\) is less than 1, or start or end is negative. Otherwise it does exactly one of three things, based on \(n \bmod 3\):

- If \(n \bmod 3 = 0\): it calls \(func(start+1, end)\), which has new length \(n-1\), and the returned value is passed through unchanged.
- If \(n \bmod 3 = 1\): it calls \(1 + func(start, end-1)\), which has new length \(n-1\), and 1 is added to whatever that call returns.
- If \(n \bmod 3 = 2\): it calls \(func(start+2, end)\), which has new length \(n-2\), and the returned value is passed through unchanged.

Because start and end can always be chosen large enough (for example \(start = 0\), \(end = n-1\)) so that the negative-index check never fires before \(n\) itself drops below 1, the returned value depends only on \(n\). Call this value \(f(n)\).

Step 2: Compute f(n) for small values.

\(f(0) = 0\)
\(f(1) = 1 + f(0) = 1\) (since \(1 \bmod 3 = 1\))
\(f(2) = f(0) = 0\) (since \(2 \bmod 3 = 2\))
\(f(3) = f(2) = 0\) (since \(3 \bmod 3 = 0\))
\(f(4) = 1 + f(3) = 1\) (since \(4 \bmod 3 = 1\))
\(f(5) = f(3) = 0\)
\(f(6) = f(5) = 0\)
\(f(7) = 1 + f(6) = 1\)

Step 3: Prove the pattern never exceeds 1. Whenever \(n\) is a multiple of 3, the chain of reductions that follows only ever visits lengths congruent to 0 or 2 modulo 3 (it goes \(3k \to 3k-1 \to 3k-3 \to 3k-4 \to \ldots \to 0\)), so it never again lands on a length congruent to 1 modulo 3. This means the plus-one branch can fire at most once in the entire recursion, only at the very first call if the original length is congruent to 1 modulo 3. Every subsequent step, once the length becomes a multiple of 3, contributes nothing further.

Step 4: Conclusion. Therefore \(f(n)\) can only ever be 0 or 1, no matter how large \(n\) is or what start and end are chosen. Taking \(start = 0, end = 0\) gives \(n = 1\), and \(func(0,0) = 1 + func(0,-1) = 1 + 0 = 1\), which attains this maximum.

Hence, the maximum possible value that can be returned from this function is:

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

Top GATE CS Computer Science and IT Engineering Questions

View More Questions

Top GATE CS Programming in C Questions