Search⌘ K
AI Features

Example: Time Complexity of an Algorithm With Nested Loops

Explore how to analyze time complexity in algorithms containing nested loops using C++. Learn to break down primitive operations and use Big O notation to understand algorithm efficiency.

In the previous lesson, we learned how to calculate the time complexity of an algorithm that involves a loop. Now, we’ll extend the same idea to analyzing an algorithm with ...

C++
int main(){
int n = 5;
int m = 7;
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
sum += 1;
}
cout << sum;
return 0;
}

It is a simple piece of code that prints the number of times the increment statement runs throughout the program. Let’s compute its time complexity.

Time Complexity

Let’s take the training wheels off and jump straight to line number 5. From the previous lesson, you would recall that it accounts for 6n+46n + ...