Search⌘ K
AI Features

Example: Time Complexity of an Algorithm With Nested Loops

Understand how to analyze the time complexity of algorithms involving nested loops in C++. Explore counting primitive operations and how loop nesting impacts total complexity.

A Program With Nested for Loops

Consider the following C++ program:

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 ...