Recursion on Arrays and Strings in DSA C - Time & Space Complexity
When using recursion on arrays or strings, we want to know how the time needed grows as the input gets bigger.
We ask: How many steps does the recursion take as the array or string length increases?
Analyze the time complexity of the following code snippet.
int sumArray(int arr[], int n) {
if (n == 0) {
return 0;
}
return arr[n - 1] + sumArray(arr, n - 1);
}
This function adds all elements of an array by calling itself with a smaller size each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call reducing array size by one each time.
- How many times: Exactly n times, where n is the array length.
Each recursive call does one addition and calls itself with one less element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions and 10 calls |
| 100 | 100 additions and 100 calls |
| 1000 | 1000 additions and 1000 calls |
Pattern observation: Operations grow directly with input size; doubling input doubles work.
Time Complexity: O(n)
This means the time to complete grows in a straight line with the size of the array or string.
[X] Wrong: "Recursion is always slower and more complex than loops."
[OK] Correct: Recursion here simply replaces a loop doing the same work; both have similar time complexity.
Understanding recursion time helps you explain your solutions clearly and choose the best approach confidently.
"What if we changed the recursion to process two elements per call instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion concept
Recursion means a function calls itself to break down a problem into smaller parts.Step 2: Apply to arrays or strings
For arrays or strings, recursion processes one element and then calls itself on the rest.Final Answer:
A function calls itself to solve smaller parts of the problem -> Option AQuick Check:
Recursion = function calls itself [OK]
- Confusing recursion with loops
- Thinking recursion sorts data automatically
- Believing recursion copies data
Solution
Step 1: Identify base case role
The base case stops recursion when no more characters remain, usually length 0.Step 2: Check options for correct base case
if (length == 0) return; correctly returns when length is 0, stopping recursion safely.Final Answer:
if (length == 0) return; -> Option BQuick Check:
Base case stops recursion at empty string [OK]
- Using incorrect base case conditions
- Using continue instead of return
- Checking for negative length which is invalid
void printReverse(char *str) {
if (*str == '\0') return;
printReverse(str + 1);
printf("%c", *str);
}
int main() {
char s[] = "abc";
printReverse(s);
return 0;
}Solution
Step 1: Understand recursion flow
The function calls itself moving forward in the string until it hits '\0' (end).Step 2: Print characters on return
After reaching end, it prints characters backward as recursion unwinds: 'c', 'b', 'a'.Final Answer:
cba -> Option AQuick Check:
Recursion prints string in reverse [OK]
- Expecting normal order output
- Confusing base case with print order
- Thinking code has syntax errors
int countChars(char *str) {
if (*str == '\0')
return 0;
return 1 + countChars(str);
}Solution
Step 1: Check base case correctness
Base case correctly returns 0 when string ends.Step 2: Analyze recursive call
Recursive call uses same pointer 'str' without moving forward, causing infinite recursion.Final Answer:
Recursive call does not move forward in string -> Option CQuick Check:
Pointer must advance in recursion [OK]
- Forgetting to advance pointer
- Changing return type unnecessarily
- Replacing recursion with loops without reason
Solution
Step 1: Understand palindrome check logic
Palindrome means first and last characters must match, then check inside substring.Step 2: Apply recursion on substring
Recursively check smaller substring by removing first and last characters each time.Final Answer:
Compare first and last characters, then recurse on substring excluding them -> Option DQuick Check:
Check ends and recurse inside for palindrome [OK]
- Reversing string unnecessarily
- Counting characters instead of comparing ends
- Sorting string which changes order
Ready to prove it?
Pass the test and this pattern moves unknown
