Recursion Problems in C++

1. Last Occurrence of Element (Important)

Problem: Find the last occurrence index of an element in an array/vector.

Method 1 (Left to Right): Traverse from left to right, continuously update the result when element is found

Method 2 (Right to Left): Traverse from right to left using recursion, return first match found

Method 3 (STL): Use find() with reverse iterators

Time Complexity: O(n) for all methods

Space Complexity: O(n) for recursive methods due to call stack


2. Reverse String Using Recursion (Important)

Problem: Reverse a string using recursive approach.

Method 1 (Extra Space): Build reversed string by appending characters from end to start

Method 2 (In-Place): Use two pointers (start and end), swap characters and recurse with updated pointers

Time Complexity: O(n)

Space Complexity: O(n) for method 1, O(1) extra space for method 2 (excluding recursion stack)


3. Palindrome Check (Important)

Problem: Check if a string is palindrome using recursion.

Approach: Use two pointers from start and end, compare characters and move inward recursively

Base Case: When i >= j, return true (all characters matched)