Page cover

โ˜ ๏ธRecursion

Q1: Head-Recursion

void headRecursion(int count) {
    
    // Base-Condition
    if (count == 0)
        return;

    // Recursive Relation
    headRecursion(count - 1);
    
    // Processing
    cout << count << " ";
}

// o/p = 1 2 3 4 5

Q2: Tail-Recursion


void tailRecursion(int count) {
    // Base-Condition
    if (count == 0)
        return;
   
    // Processing
    cout << count << " ";
    
    // Recursive Relation
    tailRecursion(count - 1); 
}

// o/p = 5 4 3 2 1

Q4: Climbing Stairs --> TLE Error

Q5: SayDigits

Q6: isSorted-Array

Day-04 Recursion

In this program, the recursiveFunc() function is called recursively until the base case is reached. The base case is when s > e. Therefore, the number of recursive calls made by the function is equal to half the length of the string. Hence, the time complexity of your program is O(n/2) or O(n) 1.

The space complexity of your program depends on the maximum depth of recursion. In your program, the maximum depth of recursion is equal to half the length of the string. Therefore, the space complexity of your program is O(n/2) or O(n) 2.

Q10: Find Exponent in logarithmic time

Last updated

Was this helpful?