
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
Total Unique Visitors