1.
Q-4: Given the following code fragment, what is its Big O running time?
test = 0
for i in range(n):
for j in range(n):
test = test + i * j
- O(n)
- In an example like this you want to count the nested loops. especially the loops that are dependent on the same variable, in this case, n.
- O(n^2)
- A singly nested loop like this is O(n^2)
- O(log n)
- log n typically is indicated when the problem is iteratvely made smaller
- O(n^3)
- In an example like this you want to count the nested loops. especially the loops that are dependent on the same variable, in this case, n.