Master theorem is used to determine the Big – O upper bound on functions which possess recurrence, i.e which can be broken into sub problems.
Master Theorem For Subtract and Conquer Recurrences:
Let T(n) be a function defined on positive n as shown below:
for some constants c, a>0, b>0, k>=0 and function f(n). If f(n) is O(n^{k}), then
1. If a<1 then T(n) = O(n^{k})
2. If a=1 then T(n) = O(n^{k+1})
3. if a>1 then T(n) = O(n^{k}a^{n/b})
Proof of above theorem( By substitution method ):
From above function, we have:
T(n) = aT(nb) + f(n)
T(nb) = aT(n2b) + f(nb)
T(n2b) = aT(n3b) + f(n2b)
Now,
T(nb) = a^{2}T(n3b) + af(n2b) + f(nb)
T(n) = a^{3}T(n3b) + a^{2}f(n2b) + af(nb) + f(n)
T(n) = Σ^{i=0 to n }a^{i} f(nib) + constant, where f(nib) is O(nib)
T(n) = O(n^{k} Σ^{i=0 to n/b }a^{i} )
Where,
If a<1 then Σ^{i=0 to n/b }a^{i} = O(1), T(n) = O(n^{k})
If a=1 then Σ^{i=0 to n/b }a^{i} = O(n), T(n) = O(n^{k+1})
If a>1 then Σ^{i=0 to n/b }a^{i} = O(a^{n/b}), T(n) = O(n^{k}a^{n/b})
Consider the following program for nth fibonacci number:
C++
#include<stdio.h> int fib( int n) { if (n <= 1) return n; return fib(n1) + fib(n2); } int main () { int n = 9; printf ( "%d" , fib(n)); getchar (); return 0; } 
Python3
# Python3 code for the above approach def fib(n): if (n < = 1 ): return n return fib(n  1 ) + fib(n  2 ) # Driver code n = 9 print (fib(n)) # This code is contributed # by sahishelangia 
Java
//Java code for above the approach. class clg { static int fib( int n) { if (n <= 1 ) return n; return fib(n 1 ) + fib(n 2 ); } // Driver Code public static void main (String[] args) { int n = 9 ; System.out.println( fib(n)); } } // This code is contributed by Mukul Singh. 
C#
// C# code for above the approach. using System; class GFG { static int fib( int n) { if (n <= 1) return n; return fib(n  1) + fib(n  2); } // Driver Code public static void Main(String[] args) { int n = 9; Console.WriteLine(fib(n)); } } // This code has been contributed // by RajputJi 
PHP
<?php // PHP code for the above approach function fib( $n ) { if ( $n <= 1) return $n ; return fib( $n  1) + fib( $n  2); } // Driver Code $n = 9; echo fib( $n ); // This code is contributed // by Akanksha Rai ?> 
Output
34
Time complexity Analysis:
The recursive function can be defined as, T(n) = T(n1) + T(n2)
 For Worst Case, Let T(n1) ≈ T(n2)
T(n) = 2T(n1) + c
where,f(n) = O(1)
∴ k=0, a=2, b=1;T(n) = O(n^{0}2^{n/1})
= O(2^{n})  For Best Case, Let T(n2) ≈ T(n1)
T(n) = 2T(n2) + c
where,f(n) = O(1)
∴ k=0, a=2, b=2;
T(n) = O(n^{0}2^{n/2})
= O(2^{n/2})
More Examples:

Example1:
T(n) = 3T(n1), n>0
= c, n<=0Sol:a=3, b=1, f(n)=0 so k=0;
Since a>0, T(n) = O(n^{k}a^{n/b})
T(n)= O(n^{0}3^{n/1})
T(n)= 3^{n} 
Example2:
T(n) = T(n1) + n(n1), if n>=2
= 1, if n=1Sol:a=1, b=1, f(n)=n(n1) so k=2;
Since a=1, T(n) = O(n^{k+1})
T(n)= O(n^{2+1})
T(n)= O(n^{3}) 
Example3:
T(n) = 2T(n1) – 1, if n>0
= 1, if n<=0Sol: This recurrence can't be solved using above method
since function is not of form T(n) = aT(nb) + f(n)
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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 studentfriendly price and become industry ready.
Recommended Posts:
 Advanced master theorem for divide and conquer recurrences
 Regularity condition in the master theorem.
 Analysis of Algorithm  Set 4 (Solving Recurrences)
 Algorithms  Recurrences  Set 1
 Algorithms Sample Questions  Recurrences  Set 2
 Paranthesis Theorem
 Code Optimization Technique (logical AND and logical OR)
 Generate an array of given size with equal count and sum of odd and even numbers
 Asymptotic Analysis and comparison of sorting algorithms
 Analysis of algorithms  little o and little omega notations
 Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
 Different types of recurrence relations and their solutions
 Find and Count total factors of coprime A or B in a given range 1 to N
 Find a Symmetric matrix of order N that contain integers from 0 to N1 and main diagonal should contain only 0's
 Difference between Deterministic and Nondeterministic Algorithms
 Lower and Upper Bound Theory
 Count pairs (i,j) such that (i+j) is divisible by A and B both
 Difference between Recursion and Iteration
 Find the repeating and the missing number using two equations
 Find smallest number K such that K % p = 0 and q % K = 0