Skip to content
GATEiit

GATE CSE 2026 Session 2 Q61: Maximum value returned by a recursive function on length mod 3

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.

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 1 (official GATE 2026 final answer key)

Concept used: Track how each call changes length mod 3 and when a 1 is added.

First step: From length ≡ 0 the next length is ≡ 2, and from ≡ 2 it is ≡ 0 again; see whether length ≡ 1 can ever recur.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2026 Session 2 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key