Skip to main content

GATE CSE 2025Programming in C › Recursion & Program TracingVerified

GATE CSE 2025 PYQ: #include <stdio.h> int foo ( int S [], int size ) { if ( size == 0 ) return 0 ; if ( size == 1 ) return 1 ;…

 

  1. #include <stdio.h>
  2. int foo(int S[],int size) {
  3. if(size == 0) return 0;
  4. if(size == 1) return 1;
  5. if(S[0] != S[1]) return 1+foo(S+1,size-1);
  6. return foo(S+1,size-1);
  7. }
  8. int main() {
  9. int A[]={0,1,2,2,2,0,0,1,1};
  10. printf("%d",foo(A,9));
  11. return 0;
  12. }

The value printed by the given CC program is ___________. (Answer in integer)

    Answer

    5

    Approach

    Interpret the recursive function as counting contiguous runs (blocks of equal adjacent values) in the array. Each adjacent unequal pair contributes +1 boundary, and the base case size == 1 contributes +1 for the final run.

    The recursion counts the number of contiguous equal-value groups in the array. The given array has 5 such groups, so the printed value is 5.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.