Time Complexity loop inside a loop with increased starting index

Viewed 98

I know the main concepts of time complexity questions but I always fall on these tricky ones, can someone please explain the thought process on why the answer is this one.

Also in this case the first index of the inner loop is multiplied by 3 each iteration, what was the answer to the question if the starting index of the inner loop remained the same and the step of each iteration was multiplied by 3

for j in range(1, (n**3) + 1, i * 3) :

enter image description here

1 Answers

Could be wrong on the math here, but...

The first loop is O(n^6) for obvious reasons, but the inner loop doesn't contribute significantly to the complexity as n^3 = n^6/n^3 in addition to shortening as i increases, making it n^3/3. Furthermore, at some point i*3 will already exceed n^3 and the inner loop won't even trigger.

All that would seem to suggest a negligible (in Big-O terms) contribution to the complexity, hence the complexity equaling that of the outer loop as n tends toward infinity.

As for the second question, keeping the starting index constant would increase the complexity by a factor of n^3, thus n^6 * n^3 = n^9, if I'm not mistaken.

Related