From 805bbf06a09bdfdddd98054c1f580036b8ab79b5 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Mon, 9 Jun 2025 22:01:27 +0100 Subject: [PATCH 01/15] reduce number of iterations --- .../calculateSumAndProduct.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..3678485 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -16,7 +16,7 @@ * @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 +27,24 @@ export function calculateSumAndProduct(numbers) { product *= num; } + return { + sum: sum, + product: product, + }; +}*/ +export function calculateSumAndProduct(numbers) { + let sum = 0; + let product = 1; + + for (const num of numbers) { + sum += num; + product *= num; + } + return { sum: sum, product: product, }; } + +// this solution iterates once instead of two From b6d427a717952d86806c5bdec55ac37d0b898342 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Mon, 9 Jun 2025 22:20:59 +0100 Subject: [PATCH 02/15] write complexities --- .../calculateSumAndProduct/calculateSumAndProduct.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 3678485..58b3d40 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,9 @@ * "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 From 2c0092ab1939ff044a554e65c6362ac63775b7d8 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Mon, 9 Jun 2025 22:31:30 +0100 Subject: [PATCH 03/15] Optimize findCommonItems by replacing includes() with Set lookup --- .../findCommonItems/findCommonItems.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..67e226c 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,28 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity:O(n * m) + * Space Complexity: O(k) k<=n + * Optimal Time Complexity: O(n+m+k) * * @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))), -]; +];*/ + +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) +}; + From cfad2dd6eaec3862c5a85db0f8f5af0b7f73a6d7 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 08:00:37 +0100 Subject: [PATCH 04/15] add comment for space complexity --- .../calculateSumAndProduct/calculateSumAndProduct.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 58b3d40..1bca98e 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -10,7 +10,7 @@ * } * * Time Complexity: O(n) - * Space Complexity: O(1) + * Space Complexity: O(1) we are only using two variables(sum,product) in our solution * Optimal Time Complexity: O(n) * * @param {Array} numbers - Numbers to process @@ -36,7 +36,7 @@ export function calculateSumAndProduct(numbers) { let sum = 0; let product = 1; - for (const num of numbers) { + for (const num of numbers) { //O(n) sum += num; product *= num; } From 59f98f84f778f646c632c4a1533af8dcabbac187 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 09:32:18 +0100 Subject: [PATCH 05/15] refactore --- .../JavaScript/findCommonItems/findCommonItems.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 67e226c..c95546f 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,9 +1,12 @@ /** * Finds common items between two arrays. * - * Time Complexity:O(n * m) - * Space Complexity: O(k) k<=n - * Optimal Time Complexity: O(n+m+k) + * Time Complexity:O(n * m) because for each element in firstArray, the includes method does a linear search 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 @@ -13,6 +16,8 @@ ...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(); From 8035dfa90c0ad8cccea1977a0e1a15995f4d47aa Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 09:42:08 +0100 Subject: [PATCH 06/15] correct explanation --- .../JavaScript/calculateSumAndProduct/calculateSumAndProduct.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 1bca98e..c89e4f8 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -10,7 +10,7 @@ * } * * Time Complexity: O(n) - * Space Complexity: O(1) we are only using two variables(sum,product) in our solution + * Space Complexity: O(1) we are only using two variables(sum,product) and the space does not grow with input * Optimal Time Complexity: O(n) * * @param {Array} numbers - Numbers to process From d62534f52c3623fd9b2ebe6e400779831bfb615e Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 09:45:12 +0100 Subject: [PATCH 07/15] optimise hasPairWithSum to reduce the time complexity --- .../hasPairWithSum/hasPairWithSum.js | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..9773c48 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^2) there are two 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,21 @@ 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; } From 9b1e49479b15e16b9c5653c085b59eae68d1bea9 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 11:27:59 +0100 Subject: [PATCH 08/15] optimise removeDuplicates to reduce time complexity --- .../removeDuplicates/removeDuplicates.mjs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..c5c6511 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^2) 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,22 @@ 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; } + From 87abe1917c3b8425f247495cd3564a63f7b1ccc6 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 11:35:28 +0100 Subject: [PATCH 09/15] edit explanation --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 2 +- Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 9773c48..1c180a9 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,7 +1,7 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity:O(n^2) there are two for loops + * Time Complexity: O(n²) there are two for loops * Space Complexity: O(1) becuae we are not using any extra space * * @param {Array} numbers - Array of numbers to search through diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index c5c6511..9201d6a 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,7 +1,7 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: O(n^2) because we have two nested loops + * 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 From 87af841be40ff30a7f813773343f6e5b05699adb Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Tue, 10 Jun 2025 11:56:01 +0100 Subject: [PATCH 10/15] add more explanation for clarity --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 1 + Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 1 + Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 1 + 3 files changed, 3 insertions(+) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index c95546f..65856e2 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -31,3 +31,4 @@ export const findCommonItems = (firstArray, secondArray) => { return [...seen]; //)(k) }; +// this solution is more optimised as it reduces Time Complexity. The time complexity of the inital function was exponential but now it's linear \ No newline at end of file diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 1c180a9..75e1ff4 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -34,3 +34,4 @@ export function hasPairWithSum(numbers, target) { return false; } +// the time complexity of original solution was exponential but it's linear for the second solution \ No newline at end of file diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 9201d6a..6323a45 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -49,4 +49,5 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } +// the time complexity of original solution was exponential but the it's linear for the second solution From 89203c1ce8f19803170ab5b02b7584588796224c Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Wed, 11 Jun 2025 22:01:19 +0100 Subject: [PATCH 11/15] enhance explantion for calculateSumAndProduct --- .../calculateSumAndProduct/calculateSumAndProduct.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index c89e4f8..19bbbcb 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -10,8 +10,7 @@ * } * * Time Complexity: O(n) - * Space Complexity: O(1) we are only using two variables(sum,product) and the space does not grow with input - * Optimal 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 @@ -32,6 +31,8 @@ product: product, }; }*/ + +//Optimal Time Complexity: O(n) export function calculateSumAndProduct(numbers) { let sum = 0; let product = 1; From fea06108ec3277b75e4d39794a86c58b91106df9 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Wed, 11 Jun 2025 22:15:30 +0100 Subject: [PATCH 12/15] correct explantion of findCommonItems --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 65856e2..5fd41b7 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,8 +1,8 @@ /** * Finds common items between two arrays. * - * Time Complexity:O(n * m) because for each element in firstArray, the includes method does a linear search which can take up to m steps, - * so that would be O(n*m) + * 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 @@ -27,8 +27,8 @@ export const findCommonItems = (firstArray, secondArray) => { seen.add(item); } } - - return [...seen]; //)(k) + + 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 exponential but now it's linear \ No newline at end of file +// this solution is more optimised as it reduces Time Complexity. The time complexity of the inital function was quadraticO(n*m) but now it's linearO(n+m+k) \ No newline at end of file From 35a0bc9efe841c95eb97a27e917796288056977e Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Wed, 11 Jun 2025 22:20:05 +0100 Subject: [PATCH 13/15] correct explanation of findCommonItems --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5fd41b7..9b709dc 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -31,4 +31,4 @@ export const findCommonItems = (firstArray, secondArray) => { 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 quadraticO(n*m) but now it's linearO(n+m+k) \ No newline at end of file +// 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 From 2e6c09502739395b2afb1cfcbf0c41434c2ea9cd Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Wed, 11 Jun 2025 22:20:36 +0100 Subject: [PATCH 14/15] correct explanation of hasPairWithSum --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 75e1ff4..fa10899 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,7 +1,7 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: O(n²) there are two for loops + * 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 @@ -34,4 +34,4 @@ export function hasPairWithSum(numbers, target) { return false; } -// the time complexity of original solution was exponential but it's linear for the second solution \ No newline at end of file +// 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 From 0511fb76032a1a372fcbd27a43a5a4cccfa91f61 Mon Sep 17 00:00:00 2001 From: ameneh-keshavarz Date: Wed, 11 Jun 2025 22:22:32 +0100 Subject: [PATCH 15/15] correct explantion of removeDuplicates --- Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 6323a45..805852b 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -49,5 +49,5 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } -// the time complexity of original solution was exponential but the it's linear for the second solution +// the time complexity of original solution was quadratic O(n²) but the it's linear for the second solution