I wrote a script that basically finds how many boxes can fit in another(bigger) box.
I have the boxes Array with all the box sizes and the products Array with the sizes of each product's box.
let boxes = [
{label:'box1', width: 4, height: 3, length: 12},
{label:'box2', width: 6, height: 5, length: 14},
{label:'box3', width: 8, height: 6, length: 24},
];
let products = [
{name:'AudioBox3000 ', width: 2, height: 1, length: 3},
{name:'Canister1500 ', width: 5, height: 1, length: 11}
];
for(let j = 0; j < products.length; j++) // O(n)
{
createDiv('********' + products[j].name + '*********');
for (let i = 0; i < boxes.length; i++) // O(m)
{
let totalWidth = Math.floor(boxes[i].width / products[j].width);
let totalHeight = Math.floor(boxes[i].height / products[j].height);
let totalLenght = Math.floor(boxes[i].length / products[j].length);
let totalBoxes = totalWidth * totalHeight * totalLenght;
createDiv(totalBoxes + ' boxes fits on ' + boxes[i].label);
}
}
function createDiv (value) {
let div = document.createElement('div');
div.setAttribute('class', 'app');
div.innerHTML = value;
document.body.appendChild(div);
}
So is clear that for(let j = 0; j < products.length; j++) is O(n)
and for (let i = 0; i < boxes.length; i++) is also O(n)
So I'm confused if it is O(n²) or O(n).
Could you elaborate an explanation?