Solution Review: Big (O) of Nested Loop with Addition
Explore the process of calculating the Big O time complexity for nested loops that include addition operations. This lesson helps you analyze each loop's execution count, combine them methodically, and simplify the result to identify the overall time complexity as O(n squared).
We'll cover the following...
We'll cover the following...
Solution
On line 11 in the outer loop, int i=0; runs once. i<n; gets executed times, and i+=3 executed times. In the inner loop, int j=0; gets executed times in total. j<n; executes times, and j+=2 gets executed times. See the following table for a detailed line-by-line analysis of the calculation of time complexity.
int n = 10; |
|
int sum = 0; |
|
float pie = 3.14; |
|
int i=0; |
|
i<n; |