Consider the following ANSI-C function.
int func(int start, int end){
int length=end+1-start;
if((length<1)||(start<0)||(end<0)){ return(0); }
if(length%3==0){
return(func(start+1, end));
}else if(length%3==1){
return(1+func(start, end-1));
}else {
return(func(start+2, end));
}
}The maximum possible value that can be returned from this function is . (answer in integer)
Note: Ignore syntax errors (if any) in the function.
View worked solution
Answer: 1:1
Let represent the length of the interval. We can analyze the behavior of the function based on :
- Case 1:
The function returnsfunc(start+1, end). The new length is . - Case 2:
The function returnsfunc(start+2, end). The new length is . - Case 3:
The function returns . The new length is .
Let be the value returned by the function for length (assuming ):
Using the base case for :
By induction, if , and otherwise. Any boundary cases where start or end becomes negative will only trigger the base case earlier, returning .
Thus, the maximum possible value that can be returned is 1.
Free practice · Your answers on this page are not saved.
