edited by
484 views

2 Answers

Best answer
8 votes
8 votes
for(i=0;i<=n;i++) //outer
    for(i=0;i<=n;i++) //inner
        for(i=0;i<=n;i++)//innermost-loop
            print(GO);

Initially $i= 0$ for outer loop. then, inner loop gets executed and then innermost loop executes setting $i=0$ and executes for $n+1$ times as $i= 0$ to $i = n$, At the end when $i = n+1$ Innermost loop executes to be false. then $i++$ makes $i = n+2$ and condition for inner loop executes to be false and then for outer loop condition is checked after checking condition which evalutes to be false. So, GO is printed $n+1$ times as $i = 0$ to $i=n+1$.

So, TC $=\color{blue}{\theta(n)}$. A further interesting question would be what is the final value of $i$.

selected by
4 votes
4 votes
it seems to be O(n).. because the same variable is used in all three loops.. so only one time initialization is performed.. and further i is incremented till i<n..

Related questions

631
views
1 answers
1 votes
Sajal Mallick asked Nov 28, 2023
631 views
Consider the problem that given a set Sof n integers and another integer x, whether or not there exist two elements in S whose sum is exactly x. What is the worst ... in dynamic programming? Then complexity should be O(n^2).How O(n logn)?
224
views
0 answers
0 votes
324
views
1 answers
0 votes
NeelParekh asked Jul 27, 2023
324 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
501
views
1 answers
2 votes
h4kr asked Dec 30, 2022
501 views
What is the correct time complexity in $\theta()$ ?