Computer Sciences > GATE 2026 SET-2 > Programming and Data Structures
Consider the following ANSI-C function.
The maximum possible value that can be returned from this function is ____________. (answer in integer)
Note: Ignore syntax errors (if any) in the function.

Correct : 1

The correct answer is 1.
The function computes a value based on the length = end + 1 - start of an interval, and recurses in three ways depending on length % 3. Crucially, only one branch ever adds to the return value:
length % 3 == 0: returns func(start+1, end) → length decreases by 1, new length % 3 == 2. No value added.
length % 3 == 2: returns func(start+2, end) → length decreases by 2, new length % 3 == 0. No value added.
length % 3 == 1: returns 1 + func(start, end-1) → length decreases by 1, new length % 3 == 0. +1 added here.
Why the maximum is 1: Once length % 3 == 1 fires and adds 1, the new length has remainder 0. The chain then alternates: mod 0 → mod 2 → mod 0 → mod 2 → ... until length drops below 1 and returns 0. The remainder never returns to 1 after the first firing. Therefore, no matter what valid starting values of start and end are chosen, the function can add 1 at most once throughout the entire recursive chain.
Example trace — func(0, 3), length = 4 (mod 1): 1 + func(0,2) → length=3(mod 0) → func(1,2) → length=2(mod 2) → func(3,2) → length=0 → return 0. Total = 1.
The correct maximum value that can be returned is 1.

Similar Questions

In C runtime environment, which one of the following is stored in heap?
#1562 MCQ
Consider the following three ANSI-C programs, P1, P2 and P3Which one of the following statements is true?
#1564 MCQ
Consider the following ANSI-C program. The output of this program is ____________. (answer in integer) Note: Assume that the program compiles and runs succ...
#1606 Fill in the Blanks

Related Topics

GATE CS 2026 Set-2 Q61 C function maximum return value ANSI-C recursive function GATE 2026 length modulo 3 recursion GATE programming data structures GATE 2026 func start end recursive GATE integer return value C function GATE

Unique Visitor Count

Total Unique Visitors

Loading......