diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..2009f220 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,26 +9,27 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ export function calculateSumAndProduct(numbers) { let sum = 0; - for (const num of numbers) { - sum += num; - } - let product = 1; for (const num of numbers) { - product *= num; + sum += num; + product *= num } - return { sum: sum, product: product, }; } +//Time Complexity: O(n) The loop visits each element exactly once +// Space complexity is O(1) he function uses a fixed number of variables, regardless of the input size. +// The returned object also has a fixed size. +// Optimal time complexity is O(n) because every element must be examined +// to guarantee the correct sum and product. \ No newline at end of file diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..e4fcd4ce 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,32 @@ /** * Finds common items between two arrays. - * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * + * Time Complexity: O(n * m) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n + m) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +export const findCommonItems = (firstArray, secondArray) => { + const allowedItems = new Set(secondArray); + + const uniqueValues = [ + ...new Set(firstArray.filter((item) => allowedItems.has(item))), + ]; + return uniqueValues; +}; + +console.log(findCommonItems([2, 5, 7], [5, 6, 7])); + + +// Time Complexity: O(n + m) on average. +// Building the Set takes O(m), and filtering the first array takes O(n) +// on average because Set.has() provides efficient lookups. + +// Space Complexity: O(n + m). +// The Set stores elements from secondArray, while the filter result +// and the Set of unique common items require additional space. + +// Improvement: Reduces average time complexity from O(n * m) to O(n + m). \ No newline at end of file diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..01b4ff87 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -4,18 +4,29 @@ * Time Complexity: * Space Complexity: * Optimal Time Complexity: + * + * Time Complexity: O(N^2) +// Space Complexity: O(1) +// Optimal Time Complexity: O(N) * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find * @returns {boolean} True if pair exists, false otherwise */ export function hasPairWithSum(numbers, target) { - for (let i = 0; i < numbers.length; i++) { - for (let j = i + 1; j < numbers.length; j++) { - if (numbers[i] + numbers[j] === target) { - return true; - } - } + const seenNumbers = new Set(); + + for (const currentNumber of numbers) { + const complement = target - currentNumber; + + if (seenNumbers.has(complement)) return true; + + seenNumbers.add(currentNumber); } return false; + } + + +// Time Complexity: O(n) We check each number once. +// Space Complexity: O(n) The Set may store all n numbers. \ No newline at end of file diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..ec062422 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -9,28 +9,21 @@ * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { - const uniqueItems = []; - for ( - let currentIndex = 0; - currentIndex < inputSequence.length; - currentIndex++ - ) { - let isDuplicate = false; - for ( - let compareIndex = 0; - compareIndex < uniqueItems.length; - compareIndex++ - ) { - if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { - isDuplicate = true; - break; - } - } - if (!isDuplicate) { - uniqueItems.push(inputSequence[currentIndex]); - } - } + return [...new Set(inputSequence)] - return uniqueItems; } +// Original complexity: +// The original approach typically uses nested loops to compare +// each value with the remaining values in the sequence. +// This results in O(n²) time complexity because each value +// may be compared with every other value. + + +// Refactor: +// Use a Set to remove duplicate values while preserving the +// order of their first occurrence. +// The Set stores only unique values, and spreading the Set +// into a new array creates the result. +// Set insertion is O(1) on average, so processing all n +// elements takes O(n) time overall. \ No newline at end of file