Last Updated : 19 Nov, 2018

Consider the following two recurrence relations T(n) and S(m):

\"\"

\"\"

What will be complexity of above relations?
(A) O(log2n) and O(log2m) respectively
(B) O(n) and O(log2m) respectively
(C) O(log2n) and O(m) respectively
(D) O(n) and O(m) respectively


Answer: (C)

Explanation: For recurrence relation T(n):

T(n) = T(n / 2) + C
= T(n / 4) + C + C
= T(n / 8) + 3*C
.
.
. 
N will be divided until it becomes 1. So,

= T(n / 2k) + (k-1) * C
That means, n / 2k = 1 
= k = log2n 

T(n) = 1 + (log2n - 1) * C
T(n) = O(log2n) 

And, for recurrence relation S(m):

S(m) = T(m / 2) + m 
= T(m / 4) + m / 2 + m
= T(m / 8) + m / 4 + m / 2 + m
.
.
.
.
m will be divided until it becomes 1
S(m) = T( m / 2k) + m / 2k - 1 + m / 2k - 2 ... + 1
= 1 + m *[ (1 / 2)k - 1 + (1 / 2)k - 2 + (1 / 2)k - 3 ... + 1]
= 1 + m * [ 1 + 1 / 2 + 1 / 4 + 1 / 8 + 1 / 16 ... + 1 / 2k - 1 ]
= 1 + m * [ 1 * (1 - 1/2k - 1) / 1 - 1/2]
= 1 + m * [ 1 * (1 - 1/2log2m - 1) / 1 - 1/2]
= 1 + m * [ 2 * ( 1 - 21 - log2m)]
= 1 + m * [ 2 * (m - 2) / m]
= O(m) 

So, option (C) is correct.

Quiz of this Question


Share your thoughts in the comments