Computer Sciences > GATE 2025 SET-1 > Recursion

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...
Consider the following C program:
void convert(int n) {
if (n < 0)
printf(“ % d”, n);
else {
&nbs...
Consider the following C function.
int fun (int n)
{
int x=1, k;
if (n==1) return x;
for (k=1; k < n; ++k)
&...
Total Unique Visitors
Loading......