The time complexity, in Big O notation, for each function:


int recursiveFun1(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun1(n-1);
}

This function is being called recursively n times before reaching the base case so its O(n), often called linear.


int recursiveFun2(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun2(n-5);
}

This function is called n-5 for each time, so we deduct five from n before calling the function, but n-5 is also O(n). (Actually called order of n/5 times. And, O(n/5) = O(n) ).


int recursiveFun3(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun3(n/5);
}

This function is log(n) base 5, for every time we divide by 5 before calling the function so its O(log(n))(base 5), often called logarithmic and most often Big O notation and complexity analysis uses base 2.


void recursiveFun4(int n, int m, int o)
{
    if (n <= 0)
    {
        printf("%d, %d\n",m, o);
    }
    else
    {
        recursiveFun4(n-1, m+1, o);
        recursiveFun4(n-1, m, o+1);
    }
}

Here, it's O(2^n), or exponential, since each function call calls itself twice unless it has been recursed n times.



int recursiveFun5(int n)
{
    for (i = 0; i < n; i += 2) {
        // do something
    }

    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun5(n-5);
}

And here the for loop takes n/2 since we're increasing by 2, and the recursion takes n/5 and since the for loop is called recursively, therefore, the time complexity is in

(n/5) * (n/2) = n^2/10,

due to Asymptotic behavior and worst-case scenario considerations or the upper bound that big O is striving for, we are only interested in the largest term so O(n^2).


Good luck on your midterms ;)

Answer from coder on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ how-to-analyse-complexity-of-recurrence-relation
Time Complexity Analysis of Recursive Functions - GeeksforGeeks
The time complexity of this function is written as T(n) = 2T(n/2) + O(n). Now to find the time complexity, we need to solve this recurrence. There are mainly three common methods used to solve recurrence relations that arise in the analysis ...
Published: March 7, 2026
Top answer
1 of 7
595

The time complexity, in Big O notation, for each function:


int recursiveFun1(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun1(n-1);
}

This function is being called recursively n times before reaching the base case so its O(n), often called linear.


int recursiveFun2(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun2(n-5);
}

This function is called n-5 for each time, so we deduct five from n before calling the function, but n-5 is also O(n). (Actually called order of n/5 times. And, O(n/5) = O(n) ).


int recursiveFun3(int n)
{
    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun3(n/5);
}

This function is log(n) base 5, for every time we divide by 5 before calling the function so its O(log(n))(base 5), often called logarithmic and most often Big O notation and complexity analysis uses base 2.


void recursiveFun4(int n, int m, int o)
{
    if (n <= 0)
    {
        printf("%d, %d\n",m, o);
    }
    else
    {
        recursiveFun4(n-1, m+1, o);
        recursiveFun4(n-1, m, o+1);
    }
}

Here, it's O(2^n), or exponential, since each function call calls itself twice unless it has been recursed n times.



int recursiveFun5(int n)
{
    for (i = 0; i < n; i += 2) {
        // do something
    }

    if (n <= 0)
        return 1;
    else
        return 1 + recursiveFun5(n-5);
}

And here the for loop takes n/2 since we're increasing by 2, and the recursion takes n/5 and since the for loop is called recursively, therefore, the time complexity is in

(n/5) * (n/2) = n^2/10,

due to Asymptotic behavior and worst-case scenario considerations or the upper bound that big O is striving for, we are only interested in the largest term so O(n^2).


Good luck on your midterms ;)

2 of 7
153

For the case where n <= 0, T(n) = O(1). Therefore, the time complexity will depend on when n >= 0.

We will consider the case n >= 0 in the part below.

1.

T(n) = a + T(n - 1)

where a is some constant.

By induction:

T(n) = n * a + T(0) = n * a + b = O(n)

where a, b are some constant.

2.

T(n) = a + T(n - 5)

where a is some constant

By induction:

T(n) = ceil(n / 5) * a + T(k) = ceil(n / 5) * a + b = O(n)

where a, b are some constant and k <= 0

3.

T(n) = a + T(n / 5)

where a is some constant

By induction:

T(n) = a * log5(n) + T(0) = a * log5(n) + b = O(log n)

where a, b are some constant

4.

T(n) = a + 2 * T(n - 1)

where a is some constant

By induction:

T(n) = a + 2a + 4a + ... + 2^(n-1) * a + T(0) * 2^n 
     = a * 2^n - a + b * 2^n
     = (a + b) * 2^n - a
     = O(2 ^ n)

where a, b are some constant.

5.

T(n) = n / 2 + T(n - 5)

where n is some constant

Rewrite n = 5q + r where q and r are integer and r = 0, 1, 2, 3, 4

T(5q + r) = (5q + r) / 2 + T(5 * (q - 1) + r)

We have q = (n - r) / 5, and since r < 5, we can consider it a constant, so q = O(n)

By induction:

T(n) = T(5q + r)
     = (5q + r) / 2 + (5 * (q - 1) + r) / 2 + ... + r / 2 +  T(r)
     = 5 / 2 * (q + (q - 1) + ... + 1) +  1 / 2 * (q + 1) * r + T(r)
     = 5 / 4 * (q + 1) * q + 1 / 2 * (q + 1) * r + T(r)
     = 5 / 4 * q^2 + 5 / 4 * q + 1 / 2 * q * r + 1 / 2 * r + T(r)

Since r < 4, we can find some constant b so that b >= T(r)

T(n) = T(5q + r)
     = 5 / 2 * q^2 + (5 / 4 + 1 / 2 * r) * q + 1 / 2 * r + b
     = 5 / 2 * O(n ^ 2) + (5 / 4 + 1 / 2 * r) * O(n) + 1 / 2 * r + b
     = O(n ^ 2)
Discussions

Time Complexity of Recursion
I'm pretty sure that that algorithm is actually O(N), which is equivalent to your intuition that it should be O(2log N) since you can simplify that expression. There will be some number of layers of recursion from this algorithm. Since each layer divides n by 2 that sets up a logarithmic relationship: the number of layers of recursion will be proportional to log(N) (note: logs here are all binary, but typically in big-O notation change of base is free--it just introduces a constant factor that can be dropped). With each layer of the recursion there are 2k calls to func. There is nothing else in func that takes an amount of time that depends on n. Therefore the time complexity of the result will just be the number of calls to func. That's O(2log N) which simplifies to O(N). Turning to master theorem notation, this function's time cost is T(N) = 2T(N/2) + O(1). That is to say, the time to call the function once is equal to two times the time to call it on half as large of an input, plus some constant time that doesn't depend on n. Thus we take a = 2, b = 2, and f(N) = 1. From here we see that f(N) = N0 = O(N0), so c = 0. We then check that log base b of a (i.e. binary log of 2, which is 1) is greater than c, which it is: 1 > 0. That means that the end result is O(Nlog_b a) = O(N1) = O(N). More on reddit.com
๐ŸŒ r/AskProgramming
5
4
December 9, 2023
recursion - Why is the time complexity of a recursive function 2^n? - Stack Overflow
The time complexity of a recursive function with two branches is supposed to be O(2^n). Mergesort, which can be solved recursively, has a time complexity of O(n logn). What am I missing here? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Can Fibonacci numbers be calculated using recursion in O(N) without memo?
Surprised I don't see this mentioned, but there is a nifty way to do it without memoization -- in O(log N). Matrix Exponentiation. Basically, consider your processing step: you have two values, the previous and current numbers. You do a nice clean simple linear transformation, and end up with the current and next. Flip it around a bit: you have an incoming 2-vector, you do a matrix multiply, giving a new 2-vector. Doing it several times to get new numbers just stacks up a sequence of matrix multiplies, which you can re-associate to be a matrix to the Nth power times the starting vector. More thinking. If I wanted a matrix to the 8th power, I'd not multiply it eight times. I'd square it, then square that, then square that. So O(log N) matrix multiplies (this is called Russian Peasant Exponentiation, or The Egyptian Method). Up to some N, the O(N) method using fast steps will be faster than the O(log N) method which does Matrix Multiplies, but at some N, the O(log N) will pull ahead. OK, trying some Bad Text Graphics ... One FIB step, as matrix multiply: โŽก Fโ‚‚ โŽค โŽก 0 1 โŽค โŽก Fโ‚ โŽค โŽข โŽฅ := โŽข โŽฅ ร— โŽข โŽฅ โŽฃ Fโ‚ƒ โŽฆ โŽฃ 1 1 โŽฆ โŽฃ Fโ‚‚ โŽฆ Eight FIB steps: โŽก Fโ‚ˆ โŽค โŽก 0 1 โŽคโธ โŽก Fโ‚ โŽค โŽข โŽฅ := โŽข โŽฅ ร— โŽข โŽฅ โŽฃ Fโ‚‰ โŽฆ โŽฃ 1 1 โŽฆ โŽฃ Fโ‚‚ โŽฆ So for N steps, compute the Nth power of that matrix, which can be done in O(log N). You can see a bunch of different "takes" on this method here: https://rosettacode.org/wiki/Fibonacci_matrix-exponentiation [Edit to add: looking for even faster ways to do the matrix exponentiation will lead you to looking at very cool stuff that totally does not apply to this problem. You know, just in case you are math-curious ;] More on reddit.com
๐ŸŒ r/compsci
19
11
July 3, 2024
Time Complexity of Recursive Functions
Merge sort is a true divide-and-conquer algorithm: the two recursive calls are each responsible for half of the list. A naive recursive Fibonacci implementation would be more accurately described as "duplicate-and-conquer". When calculating fib(n) and fib(n+1) for an addition, the latter call duplicates all the work of the former call. But you can have a linear recursive Fibonacci implementation by passing additional parameters: fib'(0,x,y) = x fib'(1+n,x,y) = fib'(n, x+y, x) fib(n) = fib'(n,1,0) Think of it as a dynamic programming solution filling in an array of Fibonacci numbers, but keeping only the bits of the array you need for the next step. More on reddit.com
๐ŸŒ r/algorithms
11
2
June 4, 2024
๐ŸŒ
Medium
tarunjain07.medium.com โ€บ complexity-analysis-for-recursion-notes-cd4930e26683
Complexity analysis for recursion โ€” [Notes] | by Tarun Jain | Medium
January 14, 2025 - The master theorem recurrence describes time complexity of an algorithm that divides a problem of size n into a number of subproblems, each of size n/b, where a and b are positive constants ยท Here โ€œaโ€ number of subproblems are solved ...
๐ŸŒ
Enjoy Algorithms
enjoyalgorithms.com โ€บ blog โ€บ time-complexity-analysis-of-recursion-in-programming
Analysis of Recursion in Data Structures and Algorithms
In recursion, we solve a problem by breaking it into smaller subproblems. If the time complexity function of input size n is T(n), then the time complexity of the smaller subproblems will be defined by the same function, but in terms of the subproblem's input size.
๐ŸŒ
Launch School
launchschool.com โ€บ books โ€บ advanced_dsa โ€บ read โ€บ time_and_space_complexity_recursive
Analyzing Time and Space Complexity in Recursive Algorithms
To determine the time complexity, we consider the number of operations performed as a function of the input size (n). In the case of the factorial function, the number of multiplications is directly proportional to n because we make n recursive calls, reducing the input size by 1 each time ...
๐ŸŒ
YourBasic
yourbasic.org โ€บ algorithms โ€บ time-complexity-recursive-functions
Time complexity of recursive functions [Master theorem] ยท YourBasic
T(n) = 1 + T(n/2), when n > 1. (**) The equation (**) captures the fact that the function performs constant work (thatโ€™s the one) and a single recursive call to a slice of size n/2.
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ time complexity of recursion
r/AskProgramming on Reddit: Time Complexity of Recursion
December 9, 2023 -

Hello, I'm trying to wrap my head around time complexity of this recursive code,

func(n)

if (n<10) return

func(n/2)

func(n/2)

It's time complexity is O(logn). I've looked through master theorem and all that, and can probably regurgitate the answer during the exam. But intuitively, I don't understand why its not something like 2^(logn). Since it also branches like the Fibonacci sequence, which results in O(2^n).

Top answer
1 of 4
4
I'm pretty sure that that algorithm is actually O(N), which is equivalent to your intuition that it should be O(2log N) since you can simplify that expression. There will be some number of layers of recursion from this algorithm. Since each layer divides n by 2 that sets up a logarithmic relationship: the number of layers of recursion will be proportional to log(N) (note: logs here are all binary, but typically in big-O notation change of base is free--it just introduces a constant factor that can be dropped). With each layer of the recursion there are 2k calls to func. There is nothing else in func that takes an amount of time that depends on n. Therefore the time complexity of the result will just be the number of calls to func. That's O(2log N) which simplifies to O(N). Turning to master theorem notation, this function's time cost is T(N) = 2T(N/2) + O(1). That is to say, the time to call the function once is equal to two times the time to call it on half as large of an input, plus some constant time that doesn't depend on n. Thus we take a = 2, b = 2, and f(N) = 1. From here we see that f(N) = N0 = O(N0), so c = 0. We then check that log base b of a (i.e. binary log of 2, which is 1) is greater than c, which it is: 1 > 0. That means that the end result is O(Nlog_b a) = O(N1) = O(N).
2 of 4
2
it also branches like the Fibonacci sequence, which results in O(2n) It doesn't quite. Consider a naive implementation of fib. Let's look at the call trees for some small values of N. Let's assume that fib(0) == 0, fib(1) == 1, and fib(N) = fib(N - 1) + fib(N - 2): fib(0) -> 1 call total fib(1) -> 1 call total fib(2) -> 3 calls total fib(1) fib(0) fib(3) -> 5 calls total fib(2) fib(1) fib(0) fib(1) fib(4) -> 9 calls total fib(3) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0) fib(5) -> 15 calls total fib(4) fib(3) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0) fib(3) fib(2) fib(1) fib(0) fib(1) Consider now your function, this time for say N = 5, 10, 15, 20, 25: func(5) -> 1 calls total func(10) -> 3 calls total func(5) func(5) func(15) -> 3 calls total func(7) func(7) func(20) -> 7 calls total func(10) func(5) func(5) func(10) func(5) func(5) func(25) -> 7 calls total func(12) func(6) func(6) func(12) func(6) func(6) You're right that each recursive call spawns two subproblems. But fib's recursion goes deeper; the "spine" of fibs call tree is N deep, while the "spine" of your function is only proportional to log2(N) deep.
Find elsewhere
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ data structures and algorithms in python โ€บ time complexity of recursive loops
Time Complexity of recursive loops | PrepInsta
August 1, 2025 - The time complexity depends on how many times the function calls itself and the amount of work done in each call. Itโ€™s often expressed using recurrence relations like T(n) = T(n-1) + O(1). Why is recursion sometimes less efficient than iteration?
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 63905266 โ€บ why-is-the-time-complexity-of-a-recursive-function-2n
recursion - Why is the time complexity of a recursive function 2^n? - Stack Overflow
The time complexity of a recursive function with two branches is supposed to be O(2^n). Mergesort, which can be solved recursively, has a time complexity of O(n logn). What am I missing here?
๐ŸŒ
DEV Community
dev.to โ€บ emmanuelj โ€บ understanding-the-complexity-of-recursive-functions-in-python-198m
Understanding the Complexity of Recursive Functions in Python - DEV Community
June 23, 2024 - Example of a simple recursive function ... - 1) The time complexity of a recursive function depends on the number of times the function is called and the work done at each call....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ how-to-solve-time-complexity-recurrence-relations-using-recursion-tree-method
Recursion Tree Method to Solve Recurrences - GeeksforGeeks
January 11, 2026 - The recursion tree method is used to analyze the time complexity of recursive algorithms by visually representing the recurrence as a tree. Each node of the tree represents the work done in a single recursive call, and each level represents one stage of the recursion.
๐ŸŒ
LearnYard
read.learnyard.com โ€บ dsa โ€บ recursion-time-and-space-complexity-analysis-1
Recursion: Time & Space Complexity Analysis - 1: LearnYard
January 6, 2025 - As you must have realized while thinking about the time complexity, the depth of the recursion tree will be approximately O(LogN) [to the base 2], and the extra space taken per recursive call is constant only as we aren't creating any array ...
๐ŸŒ
YouTube
youtube.com โ€บ sanket explains
How to calculate time complexity of Recursive algorithms ? Problems On Recursive Code Complexity - YouTube
In this video, we will explore the concept of time complexity for recursive algorithms and solve some problems related to recursive code complexity.Recursive...
Published: April 5, 2023
๐ŸŒ
Dot Net Tutorials
dotnettutorials.net โ€บ home โ€บ time complexity of recursive function
Time Complexity of Recursive Function - Dot Net Tutorials
March 20, 2021 - There is one more method to find the time complexity i.e. using recurrence relation. Let us see how to write a recurrence relation and how to solve it to find the time complexity of the recursive function.
๐ŸŒ
Medium
elshad-karimov.medium.com โ€บ how-to-find-time-complexity-of-recursive-function-eba3c513dce3
How to find time complexity of recursive function | by Elshad Karimov | Medium
March 16, 2023 - Determine the recurrence relation: In this function, we have one recursive call with input n - 1. Therefore, we can write the recurrence relation as follows: ... where T(n) is the time complexity of the function for input n and O(1) represents ...
๐ŸŒ
Medium
medium.com โ€บ nerd-for-tech โ€บ practically-understanding-time-complexities-of-recursive-and-iterative-functions-95238525c145
Practically understanding time complexities of Recursive and Iterative functions
May 30, 2021 - In the recursive algorithm the if statement takes constant time but the time taken by the recursive statement (recursivefib(n โ€” 1) + recursivefib(n โ€” 2) ) depends on the inputโ€ฆ
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ recursive-algorithms-and-their-time-complexities-o-n-vs-2-n-713856ad4e2
Recursive Algorithms and Their Time Complexities O(n) vs O(2^n) | by Code Ceeker | JavaScript in Plain English
June 13, 2023 - Hence, we can see, that we get 4 function calls for a factorial of 4 in the above example. In every function call, we have a constant time, we do not have any loop in our function. Hence, we can write it as below. T = O(1) => Time complexity of a single function call
๐ŸŒ
IDeserve
ideserve.co.in โ€บ learn โ€บ time-and-space-complexity-of-recursive-algorithms
Time and Space Complexity of Recursive Algorithms
Find the best information and most relevant links on all topics related to ideserve.co.in. Contact the domain owner here