diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..19bbbcb 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,14 +9,13 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) + * Space Complexity: O(1) because we are only using two variables(sum,product) and the space does not grow with input * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ -export function calculateSumAndProduct(numbers) { +/*export function calculateSumAndProduct(numbers) { let sum = 0; for (const num of numbers) { sum += num; @@ -27,8 +26,26 @@ export function calculateSumAndProduct(numbers) { product *= num; } + return { + sum: sum, + product: product, + }; +}*/ + +//Optimal Time Complexity: O(n) +export function calculateSumAndProduct(numbers) { + let sum = 0; + let product = 1; + + for (const num of numbers) { //O(n) + sum += num; + product *= num; + } + return { sum: sum, product: product, }; } + +// this solution iterates once instead of two diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..9b709dc 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,34 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity:O(n * m) because for each element in firstArray, the includes method does a linear search in the second array + * which can take up to m steps, so that would be O(n*m) + * + * Space Complexity: O(n) beacause firstArray.filter() creates an array of at most n items and Set stores at most n items, + * so the overall complexity would be O(n)+O(n) which we consider O(n) as they grow linerly not exponentially + * * * @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) => [ +/*export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +];*/ + +//Optimal Time Complexity: O(n+m+k) + +export const findCommonItems = (firstArray, secondArray) => { + const setSecond = new Set(secondArray); // O(m) + const seen = new Set(); + + for (const item of firstArray) { // )(n) + if (setSecond.has(item)) { + seen.add(item); + } + } + + return [...seen]; //)(k) this creates an array from seen which contains only the common items +}; + +// this solution is more optimised as it reduces Time Complexity. The time complexity of the inital function was quadratic O(n*m) but now it's linear O(n+m+k) \ No newline at end of file diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..fa10899 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,15 +1,14 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) there are two nested for loops + * Space Complexity: O(1) becuae we are not using any extra space * * @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) { +/*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) { @@ -17,5 +16,22 @@ export function hasPairWithSum(numbers, target) { } } } + return false; +}*/ + +//Optimal Time Complexity: O(n) + +export function hasPairWithSum(numbers, target) { + const seen = new Set(); + + for (const num of numbers) { // O(n) + const complement = target - num; + if (seen.has(complement)) { + return true; + } + seen.add(num); + } + return false; } +// the time complexity of original solution was quadratic O(n²) but it's linear for the second solution O(n) \ No newline at end of file diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..805852b 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,14 +1,13 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) because we have two nested loops + * Space Complexity: O(n) uniqueItems array stores up to n elements if all are unique * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ -export function removeDuplicates(inputSequence) { +/*export function removeDuplicates(inputSequence) { const uniqueItems = []; for ( @@ -32,5 +31,23 @@ export function removeDuplicates(inputSequence) { } } + return uniqueItems; +}*/ + +//Optimal Time Complexity: O(n) we iterate through the inputSequence once + +export function removeDuplicates(inputSequence) { + const seen = new Set(); + const uniqueItems = []; + + for (const item of inputSequence) { + if (!seen.has(item)) { + seen.add(item); + uniqueItems.push(item); + } + } + return uniqueItems; } +// the time complexity of original solution was quadratic O(n²) but the it's linear for the second solution +