All Exams Test series for 1 year @ ₹349 only
Question

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.

Analyzing the C Function `func` for Maximum Return Value

The problem asks for the maximum possible value returned by the given ANSI-C function `func(int start, int end)`. We need to analyze its recursive logic.

Function Logic Breakdown

The function calculates a $length$ and uses it to decide its recursive behavior.

  • Length Calculation: $int length = end + 1 - start;$
  • Base Case / Input Validation:
    • If $length < 1$, or $start < 0$, or $end < 0$, it returns $0$. This ensures termination and handles invalid inputs. The primary termination condition is when $end < start$, making $length$ less than 1.
  • Recursive Cases (based on $length % 3$):
    • If $length % 3 == 0$: Calls $func(start + 1, end)$. The length decreases by 1.
    • If $length % 3 == 1$: Returns $1 + func(start, end - 1)$. The length decreases by 1. Crucially, this is the only case where the return value increases by 1.
    • If $length % 3 == 2$: Calls $func(start + 2, end)$. The length decreases by 2.

Maximum Value Determination

The function's return value is incremented only in the specific case where $length % 3 == 1$. To find the maximum possible value, we need to determine how many times this condition can be met along any execution path.

State Transitions Analysis

Let's analyze the sequence of $length % 3$ values and the resulting state changes:

  • State 1: $length % 3 == 1$
    • Action: Add 1 to the result.
    • Next length: $L' = length - 1$.
    • Next $length % 3$: Since $length = 3k + 1$, $L' = 3k$. So, $L' % 3 == 0$.
  • State 0: $length % 3 == 0$
    • Action: Recurse with $start + 1$.
    • Next length: $L' = length - 1$.
    • Next $length % 3$: Since $length = 3k$, $L' = 3k - 1$. So, $L' % 3 == 2$.
  • State 2: $length % 3 == 2$
    • Action: Recurse with $start + 2$.
    • Next length: $L' = length - 2$.
    • Next $length % 3$: Since $length = 3k + 2$, $L' = 3k$. So, $L' % 3 == 0$.

Path Analysis

Consider an execution path:

  1. If the path starts in State 1 ($length % 3 == 1), it adds 1. The next state is State 0 (length becomes $L-1$).
  2. From State 0, the next state is State 2 (length becomes $L-2$).
  3. From State 2, the next state is State 0 (length becomes $L-4$).
  4. The sequence of states after the initial State 1 is $0 -> 2 -> 0 -> 2 -> ...$.

This pattern shows that once the execution leaves State 1, it never returns to State 1. Therefore, the condition $length % 3 == 1$ can be met at most once in any single execution path.

Conclusion

Since the value is incremented only when $length % 3 == 1$, and this condition occurs at most once per path, the maximum possible value that can be returned is 1.

For example, consider calling $func(0, 3)$:

  • Initial call: $func(0, 3)$. length = 3 + 1 - 0 = 4.
  • $4 % 3 == 1$. Returns $1 + func(0, 2)$. (Value = 1 so far)
  • Next call: $func(0, 2)$. length = 2 + 1 - 0 = 3.
  • $3 % 3 == 0$. Calls $func(1, 2)$.
  • Next call: $func(1, 2)$. length = 2 + 1 - 1 = 2.
  • $2 % 3 == 2$. Calls $func(1 + 2, 2)$, i.e., $func(3, 2)$.
  • Next call: $func(3, 2)$. length = 2 + 1 - 3 = 0.
  • $length < 1$. Returns 0.

The final result is $1 + 0 = 1$.

Was this answer helpful?

Important Questions from Function Recursion

  1. The process of removing recursion involves replacing recursive function calls with:

  2. #include <stdio.h>
    
    int foo(int S[], int size){
        if(size == 0) return 0;
        if(size == 1) return 1;
        if(S[0] != S[1]) return 1 + foo(S + 1, size - 1);
        return foo(S + 1, size - 1);
    }
    
    int main(){
        int A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1};
        printf("%d", foo(A, 9));
        return 0;
    }
    

    The value printed by the given C program is _______ . (Answer in integer)

  3. Consider the recursive functions represented by the following code segment:
    int bar(int n){
    if (n == 1) return 0;
    else return 1 + bar(n/2);
    }
    int foo(int n){
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
    }

    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)
    Note: Ignore syntax errors (if any) in the function.
  4. What is the value returned by the function f given below when n = 100 ?
    int f (int n)
    { if (n == 0) then return n;
    else
    return n + f(n-2);
    }
  5. What is the output of the given C language code snippet? 

    #include<stdio.h>

     void f(int n)

    {

     if(n==0)

     return; 

    printf("%d ",n); 

    f(n-1); 

    int main()

    f(3); 

    return 0; 

    }

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App