Solution: Big O of a Nested Loop with Multiplication
Explore how to analyze the time complexity of nested loops with multiplicative growth in this lesson. Learn to apply Big O notation to loops where the outer loop runs logarithmically and the inner loop grows exponentially, enabling precise complexity evaluation in coding interviews.
We'll cover the following...
Solution #
Explanation
The answer is . Have a look at the slides below for an in-depth explanation of the answer.
Note: In the slides below, “RTC” is an abbreviation of running time complexity.
Time complexity
The above slides give a detailed, step-by-step analysis of the code. Here, we provide a more summarized version.
The outer loop here runs times. In the first iteration of the outer loop, the body of the inner loop runs once. In the second iteration, it runs twice, and so on. The number of executions of the body of the inner loop increases in powers of . So, if is the number of iterations of the outer loop, the body of the inner loop runs a total of times. This geometric series sums to . The inner loop condition requires that in the last time the inner loop runs, it runs at most ...