Search⌘ K
AI Features

Example: Time Complexity of an Algorithm With Nested Loops

Explore how to calculate the time complexity of algorithms that use nested loops in JavaScript. This lesson helps you understand primitive operations within loops, how nesting affects overall complexity, and how to interpret time complexity notation for better code optimization and analysis.

A Program With Nested for Loops

Consider the following javascript program:

Node.js
//n can be anything
var n = 5
var m = 7
var sum = 0
for(var i = 0; i < n; i++)
for(var j = 0; j < m; j++)
sum += 1
console.log(sum)

It is a simple piece of code that prints the number ...