in Algorithms
177 views
3 votes
3 votes

What is the running time of the following loop?
 

Loop2(n)
p <-- 1 
for i <-- 1 to 2n do 
    p  <-- p*i 
  1. $\Theta(n^2)$
  2. $\Theta(n)$
  3. $\Theta(n\lg n)$
  4. $\Theta(n^2\lg n)$
in Algorithms
by
177 views

1 Answer

4 votes
4 votes
Best answer
Loop2(n)
p <-- 1 
for i <-- 1 to 2n do 
    p  <-- p*i 

Variable used in for loop is i. variable modify after each iteration is p actually. i remain unchange. 
So for loop will run n time and complexity will be O(n).
selected by

2 Comments

can u show the steps of execution pls?
0
0

@Arjun why it is assumed here that multiplication will take constant time? In  general, do we need to consider constant TC for multiplication?, i don't think so.

0
0
Answer:

Related questions