Which of the following codes executes the asymptotically lowest (in terms of the
ID: 3578381 • Letter: W
Question
Which of the following codes executes the asymptotically lowest (in terms of the big- estimate) number of print statements with respect to n?
A) for i := 1 to n
for j := 1 to n
print "Hello"
B) for i := 1 to n
for j := 1 to i
print "Hello"
C) for i := 1 to n
for j := 1 to i
print "Hello"
for j := i + 1 to n
print "Hello"
D) j := 1
for i := 1 to n
while j < n
print "Hello"
j := j + 1
A) for i := 1 to n
for j := 1 to n
print "Hello"
B) for i := 1 to n
for j := 1 to i
print "Hello"
C) for i := 1 to n
for j := 1 to i
print "Hello"
for j := i + 1 to n
print "Hello"
D) j := 1
for i := 1 to n
while j < n
print "Hello"
j := j + 1
Explanation / Answer
D) takes the lowest number of print statements
you can run this program in python and check the number of times hello prints
j = 1
for i in range(1,10):
while j < 10:
print ("Hello")
j = j + 1
A) nxn i.e n^2
B) nxn i.e n^2
C) nxn +n i.e n^2 +n = n^2
D) takes only n times
try below program in python and check the count number
print("****************")
c=0;
for i in range(1,10):
for j in range(1,10):
c+=1
print( c," Hello")
print("****************")
print("****************")
c=0;
for i in range(1,10):
for j in range(1,i):
c+=1
print( c," Hello")
print("****************")
c=0;
for i in range(1,10):
for j in range(1,i):
c+=1
print( c," Hello")
## below code takes O(n)
##for in range(i+1,10):
## print(c,"Hello")
print("****************")
j = 1
for i in range(1,10):
while j < 10:
print ("Hello")
j = j + 1
C)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.