You are on page 1of 5

Idea of Algorithm Efficiency (Part2)

Important Questions :
Q1: Calculate run time efficiency of the following program statement:
i=1
while i <=n:
print(i)
i=i+1
Ans: O(n)

Q2: Calculate run time efficiency of the following program segment:


i=1
while i <=n:
j=1
while j<=n:
k=1
while k<=n:
print(i,j,k)
k=k+1
j=j+1
i=i+1
Ans: O(n3)
Q3: Give a list A of appropriate size, what is the complexity of following code in teram of n ?
m=A[0]
for i in range(n):
if A[i]>m:
m=A[i]
k=0
for i in range(n):
if A[i]==m:
k=k+1
Ans: O(2n)

Q4: Given three lists A,B,C of appropriate sizes, what is the complexity of following code in
terms of n, m and p ?
for i in range(n):
for k in range(p):
x=0
for j in range(m):
x+=A[i]*B[k]

Ans: O(n*p*m)
Q5: If the efficiency of the function doit( ) can be expressed as O(n)=n 2 , calculate the
efficiency of the following program segment:
i=1
while i<=n:
j=1
while j<n:
doit(...)
j=j+1
i=i+1
Ans: O(n4)

Q6: If the function doit( ) has an efficiency factor of 5n, Calculate the run time efficiency of
the following program segment.
i=1
while i<=n:
doit(...)
i=i+1
Ans: O(5n2)
Q7:What is worst case complexity of following coode:
m=A[0]
for i in range(n):
if A[i]>m:
m=A[i]
k=0
for i in range(m):
if A[i]==m:
k=k+1

Ans: O(n+m)
Q8: If the function doit( ) has an efficiency factor of 5n, Calculate the run time efficiency of
the following program segment.
i=1
while i <=n:
j=1
while j<=n:
k=1
while k<=n:
doit(...)
k=k+1
j=j+1
i=i+1

Ans: O(5n4)

You might also like