1

This is the question-

Let $A[1,....,n]$ ba an array storing a bit $(1\,\,or\,\,0)$ at each location, and $f(m)$ is a function whose time complexity is $\theta(m)$. Consider the following program -

counter = 0;
for( i = 1; i <= n; i++)
{
    if(A[i] == 1)
        counter++;
    else
    {
        f(counter);
        counter = 0;
    }
}


Options are :

(A)$\Omega(n^2)$
(B)$\Omega(n\log n)$ and $O(n^2)$
(C)$\Theta(n)$
(D)$\mathcal{O}(n)$

The solution says - "Since it contains only one loop, so compute with linear complexity, hence the complexity will be $\Theta(n)$".

My doubt is, what if the time complexity of the function $f(m)$ was given to be $\theta(2^n)$ or of the order that grows in exponential time? Would the solution given in the book be correct in case of exponential time?

Raphael
  • 72,336
  • 29
  • 179
  • 389
  • Please try to avoid extraneous information and craft a focused question. I looks like that entire long multiple-choice problem is irrelevant, and you are asking about a different situation -- one that could be explained in a single line. Ask the question in a general form that is easiest for readers to understand and most likely to be useful to others. 2. This is already covered in our reference questions on runtime analysis; I encourage you to study them.
  • – D.W. Jan 23 '15 at 17:13
  • No, if $f$ is exponential then so will the given program be. – Tom van der Zanden Jan 23 '15 at 18:01
  • @TomvanderZanden, then why was $\theta(m)$ neglected? – Siddharth Thevaril Jan 23 '15 at 18:31
  • The explanation is misleading. I doubt the person who wrote it actually understood the situation. – Yuval Filmus Jan 23 '15 at 18:41