IAE Academy

GATE 2026 CS2 Q.61: solution and answer

GATE 2026 · CS2Q.61 · 2 marks · NAT

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.

See the question in the paper

Answer and solution

ANSWER

The maximum possible value that can be returned from func is .

Official answer: 1Source: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. Let be the value of length at a call. Track only how evolves and how many +1s the recursion adds.

  2. Also note start never decreases (it is passed as start, start+1, or start+2) and end never increases (it is passed as end or end-1). So once start >= 0 and end >= 0 hold initially, the recursion cannot terminate for a reason other than length < 1, except possibly through the one end - 1 step described below.

  3. Branch : calls func(start+1, end), giving new length . Since , the new residue is . This branch adds to the return value.

  4. Branch : calls 1+func(start, end-1), giving new length . Since , the new residue is . This branch adds exactly to the return value.

  5. Branch : calls func(start+2, end), giving new length . Since , the new residue is . This branch adds to the return value.

  6. So the residue-transition map is , , , and only the transition out of residue ever adds a point.

  7. The crucial observation: residue never appears as the TARGET of any transition — the only targets produced are (from ) and (from or ). So residue can occur only at the very first call (if the initial length is ) and can never recur later in the recursion.

  8. Hence at most one +1 can ever be earned in any run, and it is earned only when the initial length satisfies ; every subsequent step (cycling through residues and ) contributes until length < 1 triggers the base case returning .

  9. Trace check with (e.g. start = end = 0): , so it returns 1+func(start, end-1). The inner call now has length — wait, recompute: func(0,-1) has length , so length<1 (and end<0) is true and it returns . Total .

  10. Trace check with : residues go (+1) → length , residue (+0) → length , residue (+0) → length , which hits length<1 and returns . Total .

  11. Since no sequence of lengths can ever revisit residue , no run can earn a second +1. The maximum possible return value is therefore , achieved by any starting pair with .

KEY IDEA

Since start never decreases and end never increases, the recursion is driven entirely by length : the transition map is , , , and only the step ever adds +1. Because residue is never a target of any transition, it can occur only once (at the very start, if at all), so at most one point can ever be earned in the whole recursion.

WATCH OUT FOR

Assuming the +1 can be earned repeatedly (e.g. once per "cycle" of 3 in the original length) without checking which residues are actually reachable after the first step; tracing only one example instead of proving that residue can never recur rules out any larger value.

From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 9 Recursion questions, with notes

More Recursion questions