Search⌘ K
AI Features

Solution: Nested Loop With Multiplication (Intermediate)

Understand how to analyze the time complexity of nested loops with multiplication steps by breaking down iteration counts and logarithmic factors. This lesson guides you through calculating and simplifying Big O notation using a real JavaScript example, helping you gain skills crucial for coding interview success.

We'll cover the following...

Solution

Node.js
// Initializations
const n = 10;
const pie = 3.14;
let sum = 0;
var j = 1;
for (var i = 1; i < n; i += 3) {
console.log(pie);
while (j < n) {
sum = sum + 1;
j *= 3;
}
j = 1;
}
console.log(sum)
  • The outer loop index i goes: 1,4,7,10,,n ...