Search⌘ K
AI Features

Solution: Nested Loop With Multiplication (Advanced)

Explore how to evaluate the time complexity of nested loops where the inner loop doubles each iteration. Learn to derive the total complexity as O(n log n) through detailed breakdowns of loop behaviors and logarithmic calculations.

We'll cover the following...

Solution

Javascript (babel-node)
// Initializations
const n = 10;
const pie = 3.14;
let sum = 0;
for (var i = 0; i < n; i++) {
var j = 1;
console.log(pie);
while (j < i) {
sum += 1;
j *= 2;
}
}
console.log(sum);

Solution Breakdown

The outer loop is O(n)O(n) ...