GATE CS 2025 Set 1 | Question 61

Programming & Data Structures › Functions · NAT · 2 marks · Answer: 5

  1. Question 61Programming & Data Structuresnat2 marks
    Consider the C program:
    #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)

    Answer: 5

    Solution

    Answer: 5 foo scans adjacent pairs, adding 1 whenever S[0] != S[1], and adds a final 1 at the size==1 base case. This counts the number of maximal runs of equal consecutive elements (i.e. ). For the runs are:
    There are adjacent-difference positions, plus the base : .

More GATE PYQ on Functions

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2025 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →