• edited by
25,705 views
78 78 votes

Consider the following recurrence relation

$T(1)=1$

$T(n+1) = T(n)+\lfloor \sqrt{n+1} \rfloor$ for all $n \geq 1$

The value of $T(m^2)$ for $m \geq 1$ is

  1. $\frac{m}{6}\left(21m-39\right)+4$
  2. $\frac{m}{6}\left(4m^2-3m+5\right)$
  3. $\frac{m}{2}\left(3m^{2.5}-11m+20\right)-5$
  4. $\frac{m}{6}\left(5m^3-34m^2+137m-104\right)+\frac{5}{6}$

8 Answers

Best answer
102 102 votes

$T(m^2) = T(m^2-1) + \left\lfloor\sqrt{(m^2)} \right\rfloor$

               $= T(m^2 - 2) + \left\lfloor\sqrt{(m^2 - 1)} \right\rfloor +\left\lfloor\sqrt{(m^2)} \right\rfloor$

               $= T(m^2 - 3) + \left\lfloor\sqrt{(m^2 - 2)} \right\rfloor + \left\lfloor\sqrt{(m^2 - 1)} \right\rfloor +\left\lfloor\sqrt{(m^2)} \right\rfloor$

               $\vdots$

               $= T(1) + \left\lfloor\sqrt{(2)} \right\rfloor + \left\lfloor\sqrt{(3)} \right\rfloor + \ldots + \left\lfloor\sqrt{(m^2)} \right\rfloor$

               $= 3 \times 1 + 5 \times 2 +  \ldots + \left(2m - 1\right) \times (m-1) +m $

(We are taking floor of square root of numbers, and between successive square roots number of numbers are in the series $3,5,7 \dots$ like $3$ numbers from $1..4$, $5$ numbers from $5-9$ and so on).

We can try out options here or solve as shown at end:

Put $m = 5$, $T(25)  = 3 \times 1 + 5 \times 2 + 7 \times 3 + 9 \times 4 + 5 = 75$

  1. $59$
  2. $75$
  3. non-integer
  4. $297.5$


So, answer must be B.


$T(m^2) = 3 \times 1 + 5 \times 2 +  \dots + \left(2m - 1\right) \times (m-1) +m$
              $= m + \displaystyle \sum_{i=1}^{m-1} \left[ (2i+1). (i) \right] $
              $= m + \displaystyle \sum_{i=1}^{m-1} \left[2i^2 + i\right]$
              $= m + \frac{(m-1) .m .(2m-1)}{3} + \frac{(m-1)m}{2}$
              $= \frac{m}{6} \left(6 + 4m^2 -2m -4m + 2 + 3m - 3\right)$
              $= \frac{m}{6} \left(4m^2 -3m + 5\right) $

  • Sum of the first $n$ natural numbers $=\frac{n. (n+1)}{2}.$ 
  • Sum of the squares of first $n$ natural numbers $ = \frac{n. (n+1). (2n+1)}{6}.$
• edited by
84 84 votes

we can write this recurrence as T(n)=T(n-1)+√n 

Now we can apply muster theorem for subtract and conquer 

here, a=1,b=1 f(n)=O(√n )

By case 2 of  subtract and conquer T(n)=O(n^1.5)

if we take n=m^2

T(m^2)=O(m^3),and which is tighter upper bound of  option B.

see below for subtract and conquer of muster theorem.

https://www.eecis.udel.edu/~saunders/notes/recurrence-relations.pdf

16 16 votes

All options have different complexities. i.e.

$A) \:O(m^2) \:\: B) \:O(m^3)\:\: C) \: O(m^{3.5}) \: \:D) \: O(m^4)$

So, We can get time complexity T(n) from the recurrence relation and match with options. We ignore floor for worst case time complexity calculation.

$T(n+1) = T(n) + \sqrt{n+1}$

$Put\: n +1 = m^2$

$T(m^2) = T(m^2 - 1) + m$

$Put\: m^2 = p \:\: -(2)$

$T(p) = T(p - 1) + \sqrt{p} \:\:\: -(1)$

Here we have 2 methods to solve:

1st Method:

Using Subtract and Conquer(link) you will get :

$T(p) = O( p^{1.5+1} ) = O(p^{2.5}) = O(p\sqrt{p})$

$Put\: p = m^2 \:\: (using \: (2))$

$T(m^{2}) = O(m^{2} * m) = O(m^{3})$

$So\: B) \: is\: answer.$

2nd Method:

The recurrence relation (1) is sum of first p square root numbers.

Although we have floor of square root of numbers we are finding worst case time complexity so floor won't matter here.

$i.e. \: T(p) = \sqrt{1} + \sqrt{2} + ... + \sqrt{p}$

By Ramanujan’s formula for sum of the square roots of first n natural numbers (link for proof):

$T(p) = \frac{2}{3} . (\:(p-2) \sqrt{p+1} - 2\sqrt{2}) + 1$

$\therefore\: T(p) = O(p\sqrt{p})$

$Put\: p = m^2 \:\: (using \: (2))$

$T(m^{2}) = O(m^{2} * m) = O(m^{3})$

$So\: B) \: is\: answer.$

• edited by
15 15 votes

T(1)=1

T(2)=T(1)+⌊√2⌋=1+1=2

T(3)=T(2)+⌊√3⌋=2+1=3

T(4)=T(3)+⌊√4⌋=3+2=5

....... so on 

now find T(m2)

take m=2 so T(m2) =T(4)

now check options ,  only option B give value 5 which is equals to T(4)

so ans is B

6 6 votes
One easy way to solve this is to try putting different 
values of m.

For example, we know T(1) = 1. If we put m = 1, only A
and B satisfy the result.

m = 2

T(2) = T(1) + 1 = 2
T(3) = T(2) + 1 = 3
T(4) = T(3) + 2 = 5

Both A & B produce 5

m = 3
T(9) = T(4) + 2*5 + 1 = 5 + 10 + 1 = 16
Both A & B produce 16

m = 4
T(16) = T(9) + 3*7 + 1 = 16 + 21 + 1 = 38
Only B produces 38, A produces 34 which doesn't match

Source:geeksforgeeks

1 1 vote

Using Integration bounds technique we can get approximate answer.

Integration bound method is explained in detail here(page 11, theorem 9.3.1). 

$T(n) = T(n-1) + \left \lfloor \sqrt{n} \right \rfloor , n > 1$

Unrolling it gives,

$T_n = \sum_{k = 1}^{n} \left \lfloor \sqrt{k} \right \rfloor$

But, because $k - \left \lfloor k \right \rfloor < 1$

$\sum_{k = 1}^{n} \sqrt{k} - n \le T_n \le \sum_{k = 1}^{n} \sqrt{k}$

So, Let's find $S_n = \sum_{k = 1}^{n} \sqrt{k}$

Integration bound theorem: if $f$ is non decreasing function, $S = \sum_{k = 1}^{n} f(k)$ and $I = \int_{1}^{n} f(x) * dx$ then $I + f(1) \le S \le I + f(n)$.

$\frac{2}{3}*n^{\frac{3}{2}} - \frac{2}{3} + 1 \le S_n \le \frac{2}{3}*n^{\frac{3}{2}} - \frac{2}{3} + \sqrt{n}$

$\frac{2}{3}*n^{\frac{3}{2}} + \frac{1}{3} - n \le T_n \le \frac{2}{3}*n^{\frac{3}{2}} - \frac{2}{3} + \sqrt{n}$

Taking $n = m^2$

$\frac{2}{3}*m^3 - m^2  + \frac{1}{3} \le T_{m^2} \le \frac{2}{3}*m^3 - \frac{2}{3} + m$

Closet match is B.

• edited by
Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.8k
14.8k views
Kathleen asked Sep 17, 2014
14,831 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
71 71 votes
9 answers 9 answers
15.4k
15.4k views
Kathleen asked Sep 17, 2014
15,435 views
Let $\Sigma = \left\{a, b, c, d, e\right\}$ be an alphabet. We define an encoding scheme as follows:$g(a) = 3, g(b) = 5, g(c) = 7, g(d) = 9, g(e) = 11$.Let $p_i$ denote t...
92 92 votes
11 answers 11 answers
15.6k
15.6k views
Kathleen asked Sep 16, 2014
15,602 views
Let \(f : A \to B\) be an injective (one-to-one) function. Define \(g : 2^A \to 2^B\) as:\(g(C) = \left \{f(x) \mid x \in C\right\} \), for all subsets $C$ of $A$.Define ...
178 178 votes
7 answers 7 answers
28.8k
28.8k views
Kathleen asked Sep 16, 2014
28,767 views
Consider the following formula and its two interpretations \(I_1\) and \(I_2\).\(\alpha: (\forall x)\left[P_x \Leftrightarrow (\forall y)\left[Q_{xy} \Leftrightarrow \neg...