From c77732f1ce84c8a842463960934dbc766a7edf02 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 9 Feb 2026 14:16:07 +0000 Subject: [PATCH 1/4] Complexity Sum and Product --- .../calculateSumAndProduct.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..b2cad2c 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,21 +9,23 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: First and Second loop both iterate through all n elements to calculate sum and product, + * O(n) + O(n) = O(2n) = O(n), Two sequential passes through the array + * Space Complexity: O(1), Only a constant amount of space is used for the sum and product variables, regardless of input size + * Optimal Time Complexity: O(n) - We must examine each element at least once to calculate the sum and product, so O(n) is the best we can achieve for this problem. * + * we can reduce the constant factor by eliminating the redundant second loop + * Instead of two passes (2n operations), we can do both calculations in a single pass (n operations) + * * @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) { + sum += num; product *= num; } From b2eb20c18442374bb2d4f92c794255ac42f00c23 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 9 Feb 2026 15:25:14 +0000 Subject: [PATCH 2/4] complexity Find Common Items python --- .../find_common_items/find_common_items.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index 478e2ef..39bff99 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,13 +9,11 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: O(n + m) - Set creation and intersection + Space Complexity: O(n + m) - Two sets created for the input sequences + Optimal time complexity: O(n + m) - We must examine each element of both sequences at least once to find common items """ - common_items: List[ItemType] = [] - for i in first_sequence: - for j in second_sequence: - if i == j and i not in common_items: - common_items.append(i) - return common_items + + return list(set(first_sequence) & set(second_sequence)) + + From b51b5a21bf02d3152330baf21b127a29fabaea94 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:21:19 +0000 Subject: [PATCH 3/4] Complexity pair with Sum Python --- .../has_pair_with_sum/has_pair_with_sum.py | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) 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..8088903 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 @@ -7,12 +7,25 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: """ Find if there is a pair of numbers that sum to a target value. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: Single pass through the list of numbers, O(n), where n is the number of elements in the input list. Each lookup in the set is O(1) on average, so the overall time complexity is O(n). + Space Complexity: Set stores up to n elements in the worst case (if all numbers are unique), so the space complexity is O(n). + Optimal time complexity: 0(n) - We must examine each element at least once to determine if a pair exists that sums to the target, so O(n) is the best we can achieve for this problem. """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True - return False + + """seen is a set that will store the numbers we have already seen as we iterate through the list. + For each number, we calculate its complement (the value needed to reach the target sum) + and check if that complement is in the seen set. If it is, we have found a pair that sums to the target + and can return True. If not, we add the current number to the seen set and continue iterating. + If we finish iterating through the list without finding a pair, we return False.""" + + seen = set() + + for num in numbers: + complement = target_sum - num + + if complement in seen: + return True + + seen.add(num) + + return False \ No newline at end of file From 5bdf7958098a5f1ba33b0c5db01c1a461673611b Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:44:02 +0000 Subject: [PATCH 4/4] Complexity Remove Duplicate JS --- .../removeDuplicates/removeDuplicates.mjs | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..c52c71b 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,34 +1,24 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * The complexity can be reduced from O(n^2) to O(n) by using a Set to track seen items, + * instead od searching through the entire UniqueItems array each time , we can check a Set in 0(1) time + * + * Time Complexity: 0(n) Single pass through array and O(1) for Set lookups, resulting in O(n) overall + * Space Complexity: 0(n) Set and array both store up to n unique elements + * Optimal Time Complexity: 0(n) - We must examine each element at least once to determine if it's a duplicate, so O(n) is the best we can achieve for this problem. * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { + const seen = new Set(); 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]); + for (const item of inputSequence) { + if (!seen.has(item)) { + seen.add(item); + uniqueItems.push(item); } }