• edited by
11,720 views
10 10 votes

The running time of an algorithm is given by:

                      $T(n) = T(n-1) + T(n-2) - T(n-3)$, if $n > 3$

                                  = $n$, otherwise

Then what should be the relation between $T(1), T(2), T(3)$, so that the order of the algorithm is constant?

  1. $T(1) = T(2) = T(3)$
  2. $T(1) + T(3) = 2T(2)$
  3. $T(1) - T(3) = T(2)$
  4. $T(1) + T(2) = T(3)$

5 Answers

18 18 votes
For sufficiently large $n$,

$T(n) = Tn-1)+T(n-2) - T(n-3).$

If the order of the algorithm for which above recurrence is applicable for the time complexity, is a constant,

$T(n) = T(n-1)$.

$\implies T(n) = T(n) + T(n-2) - T(n-3)$

$\implies T(n-2) = T(n-3)$

Going like this, we must have $T(1) = T(2) = T(3)$ which is option A.

PS: Typo is obvious in the question.
7 7 votes
T(n)=T(n-1)+T(n-2)-T(n-3) , n>3

else T(n)=n

T(1)=1

T(2)=2

T(3)=3

using option:-

A. false

B. 1*3 is not equal to 4

C. T(1)-T(3)=> 1-3= -2 not equal to 2

D. 1+2=3 i,e equal to T(3)

ans . is D
0 0 votes

This is actually a very elegant recurrence.

T(n)=T(n−1)+T(n−2)−T(n−3)

 

For constant time complexity, we want:

                        T(n)=c

for all n.

 

So let's force the recurrence to produce a constant.

Let's Assume:

                T(n−1)=T(n−2)=T(n−3)=c

Then

                T(n)= c+c−c = c

We got it! The constant continues forever.

 

Hence we need the initial values to be equal:

                 T(1)=T(2)=T(3)

Answer:
Position:
Show:

Related questions

9 9 votes
3 answers 3 answers
7.6k
7.6k views
Arjun asked Apr 22, 2018
7,590 views
Consider the following C code segmentint f(int x) { if(x<1) return 1; else return (f(x-1)+g(x)); } int g(int x) { if(x<2) return 2; else return (f(x-1)+g(x/2)); }Of the f...
5 5 votes
4 answers 4 answers
4.0k
4.0k views
Arjun asked Apr 22, 2018
3,953 views
The time complexity of computing the transitive closure of binary relation on a set of $n$ elements is known to be$O(n)$$O(n*\log(n))$$O(n^{\frac{3}{2}})$$O(n^{3})$
5 5 votes
2 answers 2 answers
7.2k
7.2k views
Arjun asked Apr 22, 2018
7,151 views
Assume $A$ and $B$ are non-zero positive integers. The following code segment:while(A!=B){ if*(A B) A -= B; else B -= A; } cout<<A; // printing the value of AComputes the...
5 5 votes
2 answers 2 answers
8.9k
8.9k views
Arjun asked Apr 22, 2018
8,852 views
An array $A$ consists of $n$ integers in locations $A[0], A , \ldots A[n-1]$. It is required to shift the elements of the array cyclically to the left by $k$ places, wher...