Substitution Method for Recurrences (original) (raw)

Last Updated : 30 Dec, 2025

The Substitution Method is a technique used to find the time complexity of recursive algorithms by expanding the recurrence relation, identifying a pattern, and then proving the result using mathematical induction.

Let us take the below example code to understand the steps discussed above

C++ `

void fun(int n) { if (n <= 0) return;

cout << "GFG";

fun(n / 2);
fun(n / 2);

}

Java

static void fun(int n) { if (n <= 0) return;

System.out.print("GFG");

fun(n / 2);
fun(n / 2);

}

Python

def fun(n): if n <= 0: return

print("GFG", end="")

fun(n // 2)
fun(n // 2)

C#

static void Fun(int n) { if (n <= 0) return;

Console.Write("GFG");

Fun(n / 2);
Fun(n / 2);

}

JavaScript

function fun(n) { if (n <= 0) return;

process.stdout.write("GFG");

fun(Math.floor(n / 2));
fun(Math.floor(n / 2));

}

`

Understanding the Recursion for This Code

Writing the Recurrence Relation

The work done in each function call is constant (printing "GFG"), and the function makes two recursive calls of size n/2. Therefore, the recurrence relation is: T(n) = 2T(n/2) + O(1)

Applying the Substitution Method

To guess the solution of the recurrence, we analyze the number of recursive calls made by the function.
Each function call performs only constant work and splits the problem into two subproblems of size n/2.
As the recursion continues, the number of function calls doubles at each level, and the recursion stops when the input size becomes constant, which happens after about log n levels.

Hence, we guess that the time complexity is T(n) = O(n).

Now, we verify this guess.
Assume that for some constant c > 0: T(n) ≤ cn

Assume that this holds for smaller values of n, particularly for n/2: T(n/2) ≤ c(n/2)

Substituting into the recurrence relation:
T(n) = 2T(n/2) + O(1)
≤ 2 · c(n/2) + O(1)
= cn + O(1)
For sufficiently large values of n, the constant term is dominated by cn.
Thus, T(n) ≤ cn

**Result: The assumption holds true, which confirms that the time complexity of the recurrence is: T(n) = O(n)

Merge Sort Example

Please refer the Merge Sort code as the next example. In each function call, the array is divided into two halves and two recursive calls are made, each operating on a subarray of size n/2. After the recursive calls, the function performs linear work to merge the two sorted halves. Therefore, the recurrence relation is: T(n) = 2T(n/2) + n

Applying the Substitution Method

To guess the solution of the recurrence, we analyze the work done by the function at each level of recursion. Each function call divides the array into two subarrays of size n/2 and makes two recursive calls. After the recursive calls return, the function performs linear work to merge the two sorted halves.
As the recursion proceeds, the number of subproblems doubles at each level, while the size of each subproblem is halved. The recursion stops when the subarray size becomes constant, which occurs after about log n levels. At each level of the recursion tree, the total amount of work done is proportional to n.

Therefore, we guess that the time complexity is: T(n) = O(n log⁡ n)

Verifying the Guess:

Assume that for some constant c > 0: T(n) ≤ c n log n
Assume that this holds for smaller values of n, particularly for n/2: T(n/2) ≤ c (n/2) log(n/2)
Substituting into the recurrence relation:
T(n) = 2T(n/2) + n
≤ 2 ⋅ c (n/2) log(n/2) + n
= cn (log n - log 2) + n
= cn log n - cn + n

For sufficiently large values of n, the linear term is dominated by cn log n.
Thus, T(n) ≤ cn log n

**Result: The assumption holds true, which confirms that the time complexity of the recurrence is: T(n) = O(n log n)