0
$\begingroup$

what is the time complexity of:

for (i = 1; i <= n; i = 2*i)
    for (j = 0; j < i; j++)
            sum++;

? I thought it is O(nlogn) since the otter loop run logn time and the inner loop run i time

$\endgroup$
3
  • 1
    $\begingroup$ This site doesn't work that way. Help us to help you: why do you doubt your answer? what don't you understand? We're not a verifier service. $\endgroup$ Commented Oct 13, 2023 at 13:32
  • $\begingroup$ My question is what is the complexity of the provided code ? $\endgroup$ Commented Oct 13, 2023 at 17:00
  • 1
    $\begingroup$ Always helpful when you don’t have experience yet: Write a program that counts your operations and prints them. Check the output, make a guess check the guess. You’ll see it’s unlikely to be n log n. $\endgroup$ Commented Oct 17, 2023 at 16:57

1 Answer 1

3
$\begingroup$

Conside Geometric progression

$1+2+4+8+...+n=2n-1$.

So time complexity is $O(n)$.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.