AI Features

Solution: Nested Loop With Multiplication (Advanced)

This review provides a detailed analysis of the different ways to solve the Nested Loop with Multiplication challenge.

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) as it iterates n times. The inner while loop iterates i times, which is always less than n and the inner loop counter variable is doubled each time. Therefore we can say that it is O(log2(n))O(log_2(n)) ...

Ask