...

/

Solution: Big (O) of Nested Loop with Multiplication

Solution: Big (O) of Nested Loop with Multiplication

This review provides a detailed analysis of how to solve the Big (O) of Nested Loop with Multiplication Challenge.

We'll cover the following...

Solution #

Press + to interact
int main() {
int n = 10;
int sum = 0;
float pie = 3.14;
int var = 1;
while (var < n){ // O(log n)
cout << pie << endl; // O(log n)
for (int j=0; j<var; j++) // 8n - 12
sum+=1; // (4n - 6)
var*=2; // O(log n)
}
cout<<sum;
}

Time Complexity

The outer loop here runs log(n)log(n) 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 2. So, if kk ...

Access this course and 1400+ top-rated courses and projects.