For below code as there are 2 loops
,so is it correct to say Time complexity is O(N) Square
,because the code is in the form do this (which is second for loop ) each time for each element in first loop
hence multiplying the run times
Is the understanding correct?
const arr = [{ key1: 2 }, { key1: 7 }, { key1: 11 }, { key1: 15 }];
const k = 9;
let valueSet = new Set(arr.flatMap((x) => Object.values(x)));
let valueArray = [...valueSet];
let indices;
let isFound = false;
// valueArray.forEach((v1, i1) => {
for (let i1 = 0; i1 < valueArray.length && !isFound; i1++) {
for (let i2 = i1 + 1; i2 < valueArray.length && !isFound; i2++) {
if ((valueArray[i1] + valueArray[i2]) === k) {
//Return the Indices
indices = [i1, i2];
isFound = true;;
}
}
}
console.log(indices);
Regards,
Carolyn