GateAI
Recursion

GATE CSE 2025 Set 1 · Q61

NAT2 marksEasy

Time

#include <stdio.h> int foo(int S[],int size){ if(size == 0) return 0; if(size == 1) return 1; if(S[0] != S[1]) return 1+foo(S+1,size-1); return foo(S+1,size-1); } int main(){ int A[]={0,1,2,2,2,0,0,1,1}; printf("%d",foo(A,9)); return 0; }The value printed by the given C program is ________ . (Answer in integer)

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 5

Recursion

The function foo(S, size) recursively scans adjacent elements of the array:

  1. If size == 0, it returns 00.

  2. If size == 1, it returns 11 (counting the last group/element).

  3. If S[0] != S[1], it adds 11 and recurses on the rest of the array (foo(S+1, size-1)).

  4. If S[0] == S[1], it simply recurses on foo(S+1, size-1) without adding 11.

Essentially, the function counts the number of contiguous blocks/runs of identical consecutive elements (or equivalently, the number of transitions between different consecutive elements plus 11 for the final element).

Let us trace the execution for array A={0,1,2,2,2,0,0,1,1}A = \{0, 1, 2, 2, 2, 0, 0, 1, 1\} with size=9\text{size} = 9:

  • foo(A, 9): A[0]=0,A[1]=1  ⟹  0≠1  ⟹  1+foo(A+1,8)A[0] = 0, A[1] = 1 \implies 0 \neq 1 \implies 1 + \text{foo}(A+1, 8)

  • foo(A+1, 8): A[1]=1,A[2]=2  ⟹  1≠2  ⟹  1+foo(A+2,7)A[1] = 1, A[2] = 2 \implies 1 \neq 2 \implies 1 + \text{foo}(A+2, 7)

  • foo(A+2, 7): A[2]=2,A[3]=2  ⟹  2==2  ⟹  foo(A+3,6)A[2] = 2, A[3] = 2 \implies 2 == 2 \implies \text{foo}(A+3, 6)

  • foo(A+3, 6): A[3]=2,A[4]=2  ⟹  2==2  ⟹  foo(A+4,5)A[3] = 2, A[4] = 2 \implies 2 == 2 \implies \text{foo}(A+4, 5)

  • foo(A+4, 5): A[4]=2,A[5]=0  ⟹  2≠0  ⟹  1+foo(A+5,4)A[4] = 2, A[5] = 0 \implies 2 \neq 0 \implies 1 + \text{foo}(A+5, 4)

  • foo(A+5, 4): A[5]=0,A[6]=0  ⟹  0==0  ⟹  foo(A+6,3)A[5] = 0, A[6] = 0 \implies 0 == 0 \implies \text{foo}(A+6, 3)

  • foo(A+6, 3): A[6]=0,A[7]=1  ⟹  0≠1  ⟹  1+foo(A+7,2)A[6] = 0, A[7] = 1 \implies 0 \neq 1 \implies 1 + \text{foo}(A+7, 2)

  • foo(A+7, 2): A[7]=1,A[8]=1  ⟹  1==1  ⟹  foo(A+8,1)A[7] = 1, A[8] = 1 \implies 1 == 1 \implies \text{foo}(A+8, 1)

  • foo(A+8, 1): Base case size == 1 reached, returns 11.

Summing all returned values:

Result=1+1+0+0+1+0+1+0+1=5\text{Result} = 1 + 1 + 0 + 0 + 1 + 0 + 1 + 0 + 1 = 5

Thus, the printed value is 5.

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 2025 Set 1 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 2025 Set 1.

How GATE tests Recursion

More from GATE CSE 2025 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2025 Set 1 · Q61 · NAT

#include int foo(int S[],int size){ if(size == 0) return 0; if(size == 1) return 1; if(S[0] != S[1]) return 1+foo(S+1,size-1); return foo(S+1,size-1); } int main(){ int A[]={0,1,2,2,2,0,0,1,1}; printf("%d",foo(A,9)); return 0; } The value printed by the given C program is … (Answer in integer)

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.