retagged by
602 views
2 votes
2 votes

I think answer should be $O(n^{3})$

but given options are

$a.O(n^{4}) b.O(n^{5}) c.O(n^{6}) d.O(n^{7})$

What is the running time of the following code:

retagged by

1 Answer

0 votes
0 votes
For loop(Line no 1-7):

if n =1, i = 1, j = 1 time, k = 1 time
if n = 2, i = 2, j = 2 times, k = (1+2) times
if n = 3, i = 3, j = 3 times, k = (1+2+3) times
if n = n, i =n, j = n times, k = (1+2+3+-----+n) times

1+(1+2)+(1+2+3)+-----+(1+2+3+----+n)
so, time complexity = O(n^3)  

For loop(Line no. 8-12):
if n = 1, p = 1^2, q = 1^2
if n = 2, p = 2^2, q = 1+2+3+2^2
if n = 3, p = 3^2, q = 1+2+----+3^2
if n = n, p = n^2, q = 1+2+3+-----+n^2

So, time complexity  = O(n^4)

So, Time complexity of the program is = O(n^3+n^4) = O(n^4)
edited by
Answer:

Related questions

0 votes
0 votes
1 answer
1
2 votes
2 votes
1 answer
2
gauravkc asked Apr 5, 2018
897 views
What is the time complexity of this code?
3 votes
3 votes
1 answer
3
0 votes
0 votes
3 answers
4
pranab ray asked Jan 13, 2018
415 views
i am getting t.c as O(n^5) but given answer as O(n^4) what should be the answer