Search⌘ K
AI Features

Solution: Nested Loop With Multiplication (Pro)

Explore how to analyze time complexity for nested loops involving multiplication using Big O notation. Learn to calculate the total number of executions and simplify the time complexity to O(n) by considering logarithmic inner loops and constant factors.

We'll cover the following...

Solution

Node.js
const n = 10;
const pie = 3.14;
let sum = 0;
var j = 1;
for (var i = 0; i < n; i++) {
console.log(pie);
while (j < i) {
sum += 1;
j *= 2;
}
}
console.log(sum)

The outer loop in the main function has n iterations as it iterates on i from 0 to n-1. If the condition ...