From 6fbd17d99592261989749ba63d80645fe41ad42d Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Fri, 3 Oct 2025 21:53:51 +0100 Subject: [PATCH 1/4] Calculate the sum and product in JS. --- .../calculateSumAndProduct.js | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..dd92c1e 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,26 @@ * "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) + * + * It loops twice: Once to compute the sum and Once to compute the product. + * + * Time Complexity + + Let n be the number of elements in numbers. + + Operations: + + First loop: visits each element = O(n) + Second loop: visits each element again = O(n) + Even though there are two separate loops, each runs in linear time. Therefor, Time Complexity: O(n) + Space Complexity + + We're not allocating any new data structures proportional to n. + Space Complexity: O(1) + (No extra space used that grows with input size.) * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product @@ -32,3 +49,20 @@ export function calculateSumAndProduct(numbers) { product: product, }; } + + +// optimal solution + +//make it even cleaner by combining both operations into a single loop, like this. But O(n) is the best We can do for time complexity. + +export function calculateSumAndProduct(numbers) { + let sum = 0; + let product = 1; + + for (const num of numbers) { + sum += num; + product *= num; + } + + return { sum, product}; +} \ No newline at end of file From f463faa88b09981eeacbc5e3917c114b61acc451 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Fri, 3 Oct 2025 21:54:29 +0100 Subject: [PATCH 2/4] Finds common items solved in Js --- .../findCommonItems/findCommonItems.js | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..14344ed 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: - * Space Complexity: + * Time Complexity: O(n x m) + * Space Complexity: O(n) * Optimal Time Complexity: * * @param {Array} firstArray - First array to compare @@ -12,3 +12,38 @@ export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), ]; + + +/* +Time Complexity: + +n = firstArray.length +m = secondArray.length + +filter() loops over firstArray = O(n) +Inside filter, includes() searches secondArray = O(m) +Combined: O(n x m) +new Set(...) and spreading it: O(k) (where k is number of common items) +Overall Time Complexity: O(n x m) + + +Space Complexity: + +We’re creating temporary arrays and a set, all of which scale with the size of firstArray. +The overall space complexity is O(n). + +*/ + +// Optimal Solution + +export const findCommonItems2 = (firstArray, secondArray) => { + const set2 = new Set(secondArray); + return [...new Set(firstArray.filter(item => set2.has(item)))]; + +}; + +// now set2.has(item) is O(1) (instead of .includes() which is O(m)) + +//.has() is a constant-time lookup + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set From 5903a048d938e7324b13e1700e56f1a5559c4c31 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Fri, 3 Oct 2025 21:54:55 +0100 Subject: [PATCH 3/4] has_pair_with_sum Solved in Python. --- .../has_pair_with_sum/has_pair_with_sum.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index fe2da51..a08603a 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -16,3 +16,37 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: if numbers[i] + numbers[j] == target_sum: return True return False + + +''' +Time Complexity: + +Outer loop: i goes from 0 to n-1 = O(n) + +Inner loop: j goes from i+1 to n-1 = O(n) (in worst case) + +total comparison = n * (n - 1) / 2 + +Therefor total complexity = O(n²) + +Space Complexity: + +not creating new lists, sets, or hash maps. Only using loop variables i, j, and a few constants. + +Space Complexity: O(1) +''' + + +#optimal + + +def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: + seen = set() + for num in numbers: + complement = target_sum - num + if complement in seen: + return True + seen.add(num) + return False + +## Optimal for Time complexity. Reduces it to O(n), but increases the space complexity to O(n). \ No newline at end of file From 56b124e76bd7384f20df31371b51666e900fd4b8 Mon Sep 17 00:00:00 2001 From: eyuell21 Date: Fri, 3 Oct 2025 21:55:24 +0100 Subject: [PATCH 4/4] Remove duplicate values solved in Python. --- .../remove_duplicates/remove_duplicates.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..8dbe618 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -23,3 +23,33 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: unique_items.append(value) return unique_items + + +""" +Time Complexity: + +1 + 2 + 3 + ... + (n - 1) = n(n - 1)/2 = O(n²) + +Space Complexity: + +Even though we're just using .append(), the list grows linearly, so it is s O(n) space. + +""" + +#Optimal Solution + +def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: + seen = set() + unique_items = [] + + for value in values: + if value not in seen: + seen.add(value) + unique_items.append(value) + + return unique_items + +''' +Time Complexity: O(n) +Space Complexity: O(n) storing unique_items and seen +''' \ No newline at end of file