Consider the following C function.

`unsigned fun(unsigned n) ` `{ ` ` ` `if` `(n == 0) ` `return` `1; ` ` ` `if` `(n == 1) ` `return` `2; ` ` ` ` ` `return` `fun(n-1) + fun(n-1); ` `} ` |

*chevron_right*

*filter_none*

Consider the following questions for above code ignoring compiler optimization.

a) What does the above code do?

b) What is the time complexity of above code?

c) Can the time complexity of above function be reduced?

**What does fun(n) do?**

In the above code, fun(n) is equal to 2*fun(n-1). So the above function returns 2^{n}. For example, for n = 3, it returns 8, for n = 4, it returns 16.

**What is the time complexity of fun(n)?**

Time complexity of the above function is exponential. Let the Time complexity be T(n). T(n) can be written as following recurrence. Here C is a machine dependent constant.

T(n) = T(n-1) + T(n-1) + C = 2T(n-1) + C

The above recurrence has solution as Θ(2^{n}). We can solve it by recurrence tree method. The recurrence tree would be a binary tree with height n and every level would be completely full except possibly the last level.

C / \ C C / \ / \ C C C C / \ / \ / \ / \ . . . . . . . . . . . . . . . . Height of Tree is Θ(n)

**Can the time complexity of fun(n) be reduced?**

A simple way to reduce the time complexity is to make one call instead of 2 calls.

`unsigned fun(unsigned n) ` `{ ` ` ` `if` `(n == 0) ` `return` `1; ` ` ` `if` `(n == 1) ` `return` `2; ` ` ` ` ` `return` `2*fun(n-1); ` `} ` |

*chevron_right*

*filter_none*

Time complexity of the above solution is Θ(n). T Let the Time complexity be T(n). T(n) can be written as following recurrence. Here C is a machine dependent constant.

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

We can solve it by recurrence tree method. The recurrence tree would be a skewed binary tree (every internal node has only one child) with height n.

C / C / C / . . Height of Tree is Θ(n)

The above function can be further optimized using divide and conquer technique to calculate powers.

`unsigned fun(unsigned n) ` `{ ` ` ` `if` `(n == 0) ` `return` `1; ` ` ` `if` `(n == 1) ` `return` `2; ` ` ` `unsigned x = fun(n/2); ` ` ` `return` `(n%2)? 2*x*x: x*x; ` `} ` |

*chevron_right*

*filter_none*

Time complexity of the above solution is Θ(Logn). Let the Time complexity be T(n). T(n) can be approximately written as following recurrence. Here C is a machine dependent constant.

T(n) = T(n/2) + C

We can solve it by recurrence tree method. The recurrence tree would be a skewed binary tree (every internal node has only one child) with height Log(n).

C / C / C / . . Height of Tree is Θ(Logn)

We can also directly compute fun(n) using bitwise left shift operator ‘<<'.

`unsigned fun(unsigned n) ` `{ ` ` ` `return` `1 << n; ` `}` |

*chevron_right*

*filter_none*

This article is contributed by **Kartik**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Analysis of Algorithms | Set 5 (Practice Problems)
- Practice Set for Recurrence Relations
- Practice Questions on Time Complexity Analysis
- Analysis of Algorithm | Set 4 (Solving Recurrences)
- Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)
- Jump Pointer Algorithm
- Online Algorithm
- In-Place Algorithm
- Sorting algorithm visualization : Insertion Sort
- Sorting algorithm visualization : Heap Sort
- Analysis of Algorithms | Set 1 (Asymptotic Analysis)
- Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
- Analysis of Algorithms | Set 3 (Asymptotic Notations)
- NP-Completeness | Set 1 (Introduction)
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Algorithms | Recurrences | Set 1
- Sort elements by frequency | Set 5 (using Java Map)
- Algorithms Sample Questions | Recurrences | Set 2
- Algorithms Sample Questions | Set 3 | Time Order Analysis
- Find the smallest positive number missing from an unsorted array | Set 3