GateAI
Recursion

GATE CSE 2026 Set 2 · Q61

NAT2 marksMedium

Time

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.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 1

Recursion

Let the length of the interval be L=end−start+1L = \text{end} - \text{start} + 1.

Let us analyze the recursive transitions based on the value of L(mod3)L \pmod 3:

  1. Base Case: If L<1L < 1 or start<0\text{start} < 0 or end<0\text{end} < 0, the function returns 00.

  2. If L≡0(mod3)L \equiv 0 \pmod 3: The function calls func(start + 1, end). The new length becomes:

    L′=end−(start+1)+1=L−1≡2(mod3)L^{\prime} = \text{end} - (\text{start} + 1) + 1 = L - 1 \equiv 2 \pmod 3

    No value is added (+0+0).

  3. If L≡2(mod3)L \equiv 2 \pmod 3: The function calls func(start + 2, end). The new length becomes:

    L′′=end−(start+2)+1=L−2≡0(mod3)L^{\prime\prime} = \text{end} - (\text{start} + 2) + 1 = L - 2 \equiv 0 \pmod 3

    No value is added (+0+0).

  4. If L≡1(mod3)L \equiv 1 \pmod 3: The function returns 1+func(start,end−1)1 + \text{func}(\text{start}, \text{end} - 1). The new length becomes:

    L′=(end−1)−start+1=L−1≡0(mod3)L^{\prime} = (\text{end} - 1) - \text{start} + 1 = L - 1 \equiv 0 \pmod 3

    Here, 11 is added.

Notice the sequence of transitions:

  • From L≡0(mod3)L \equiv 0 \pmod 3, length becomes 2(mod3)2 \pmod 3.

  • From L≡2(mod3)L \equiv 2 \pmod 3, length becomes 0(mod3)0 \pmod 3.

  • This forms an alternating loop 0(mod3)→2(mod3)→0(mod3)→…0 \pmod 3 \to 2 \pmod 3 \to 0 \pmod 3 \to \dots until a base case is hit, adding 00 in all subsequent steps.

  • Only a state with L≡1(mod3)L \equiv 1 \pmod 3 can execute the +1 statement, and its next state immediately enters the {0,2}(mod3)\{0, 2\} \pmod 3 loop, which can never return to 1(mod3)1 \pmod 3.

Thus, the addition of 11 can occur at most once during any execution, giving a maximum return value of 1.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Recursion previous year question is Q61 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 5 of them are 2-mark NAT questions like this one, and it is the only one in GATE CSE 2026 Set 2.

How GATE tests Recursion

More from GATE CSE 2026 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 2 · Q61 · NAT

Consider the following ANSI-C function. int func(int start, int end){ int length=end+1-start; if((length The maximum possible value that can be returned from this function is … (answer in integer) Note: Ignore syntax errors (if any) in the function.

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.