edited by
74 views
1 votes
1 votes

For the below function, arr is pointing to an array of $len$ elements. The complexity of the function is

void func(int * arr, int len)
 {
     if(len == 0)
         return;
     printf("%d ", arr[0]);
     func(arr+1, len-1);
 }
  1. $O(n)$
  2. $\Theta( n^2)$
  3. $O(1)$
  4. $\Theta(n \log n)$
edited by

1 Answer

0 votes
0 votes
We can write the recurrence relation $,T(n) = \left\{\begin{matrix} T(n-1) + C&;n\geq 1 \\  C&;n = 0 \end{matrix}\right.$

$T(n) = T(n-1) + C\quad \rightarrow (1)$

$T(n-1) = T(n-2) + C$

$T(n-2) = T(n-3) + C$

$T(n-3) = T(n-4) + C$

$T(n-4) = T(n-5) + C$

$T(n-5) = T(n-6) + C$

Put all these values, in the equation $(1),$ we get

$T(n) = T(n-1) + C$

$T(n) = T(n-2) + C + C$

$T(n) = T(n-3) + C + C + C$

$T(n) = T(n-4) + C + C + C + C$

$T(n) = T(n-5) + C + C + C + C + C$

$\;\;\;\;\;\;\;\vdots \;\;\;\;\;\;\;\vdots \;\;\;\;\;\;\;\vdots$

For $k^{\text{th}}$ iteration.

$T(n) = T(n-k) + kC$

$n-k = 0,T(0) = C$

$\implies k = n$

$T(n) = C + nC = Cn = O(n).$

So, the correct answer is $(A).$
Answer:

Related questions

1 votes
1 votes
1 answer
1
gatecse asked Aug 30, 2020
152 views
Consider the following array of elements being sorted by merge sort. The sum of all the elements to which $13$ gets compared is _____$$14\; 13\; 24\; 56\; 87\; 98\; 4\; 6...
2 votes
2 votes
1 answer
3
gatecse asked Aug 30, 2020
63 views
What is the best case complexity in building a min-heap?$\Theta(n\log n)$$\Theta(n^2)$$O(\log n)$$O(n)$
1 votes
1 votes
1 answer
4
gatecse asked Aug 30, 2020
73 views
For the input $A = 11$ and $B = 10,$ the value of $F_1 \oplus F_2 \oplus F_3 = $ ($\oplus$ being the XOR operations)