Computer Sciences > GATE 2025 SET-1 > Recursion
The value printed by the given C program is ______ (Answer in integer)

Correct : 5

The output is 5.
The function foo() recursively counts the number of runs — groups of consecutive identical elements — in the array.
It works by comparing each pair of adjacent elements. If S[0] != S[1], a new run has started, so it adds 1 and recurses on the rest. If they are equal, it simply recurses without adding. The base case size == 1 returns 1 to count the last remaining run.
For the array {0, 1, 2, 2, 2, 0, 0, 1, 1}, the runs are:
• {0} → 1 run
• {1} → 1 run
• {2, 2, 2} → 1 run
• {0, 0} → 1 run
• {1, 1} → 1 run
Total = 5 runs, so foo(A, 9) prints 5.

Similar Questions

Consider the following C program: #include<stdio.h> int r(){     int static num=7;     return num--; } int main() {     for...
#239 MCQ
Consider the following C program: void convert(int n) { &nbsp; &nbsp; if (n < 0) &nbsp; &nbsp; printf(“ % d”, n); &nbsp; &nbsp; else { &nbs...
#241 MCQ
Consider the following C function. int fun (int n) { &nbsp; &nbsp; int x=1, k; &nbsp; &nbsp; if (n==1) return x; &nbsp; &nbsp; for (k=1; k < n; ++k) &...
#527 Fill in the Blanks

Related Topics

GATE CS 2025 GATE CS 2025 Set-1 Q61 C Recursion Output Count Array Runs foo Array Recursion Consecutive Equal Elements Array Runs Count C Programming GATE 2025 Recursion Trace Adjacent Elements Compare GATE CS Solved 2025 Run Length Encoding

Unique Visitor Count

Total Unique Visitors

Loading......