From 121c7aa939a98b7d5278c91d9622fd43dafef105 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sat, 14 Feb 2026 19:11:42 +0000 Subject: [PATCH 1/4] Refactor calculateSumAndProduct --- .../calculateSumAndProduct.js | 16 ++++++++-------- Sprint-1/JavaScript/package-lock.json | 12 ++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 Sprint-1/JavaScript/package-lock.json diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..8060b10 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,22 +9,22 @@ * "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) + *The original implementation used two separate loops. + *This refactor combines them into a single loop to reduce + *the constant factor, while maintaining the same Big-O complexity. + * @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; } return { diff --git a/Sprint-1/JavaScript/package-lock.json b/Sprint-1/JavaScript/package-lock.json new file mode 100644 index 0000000..37ca6f0 --- /dev/null +++ b/Sprint-1/JavaScript/package-lock.json @@ -0,0 +1,12 @@ +{ + "name": "module-complexity-sprint-1", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "module-complexity-sprint-1", + "version": "1.0.0" + } + } +} From 403c5c496c0882728ea60a9692293bad860094d5 Mon Sep 17 00:00:00 2001 From: sheida-shab Date: Sat, 14 Feb 2026 19:12:29 +0000 Subject: [PATCH 2/4] Refactor hasPairWithSum --- .../hasPairWithSum/hasPairWithSum.js | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..61e7c9c 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,30 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: - * + * Time Complexity:before refactoring O(n²)- after refactoring O(n log n) + * Space Complexity:before refactoring O(1)- after refactoring O(1) + * Optimal Time Complexity:O(n log n) + * https://www.hellointerview.com/learn/code/two-pointers/overview + * The refactored solution first sorts the array, which takes O(n log n) time, + * and then uses the two-pointer technique to scan the array in a single pass (O(n)). The sorting step dominates the overall complexity. * @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; - } - } + numbers.sort((a,b)=>a-b); + let left=0; + let right=numbers.length-1; + while (left Date: Sat, 14 Feb 2026 19:13:22 +0000 Subject: [PATCH 3/4] Refactor removeDuplicates --- .../removeDuplicates/removeDuplicates.mjs | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..f13e7dc 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +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: - * + * Time Complexity:before refactoring O(n²)- after refactoring O(n) + * Space Complexity:O(n) + * Optimal Time Complexity:O(n) + + * The original implementation relied on nested loops to detect duplicates, + *resulting in O(n²) time complexity, which does not scale well for large inputs. + *The refactored solution uses a Set to perform duplicate checks in constant time. + *This removes the need for an inner loop and reduces the overall time complexity + *to O(n), which is optimal for this problem. + *using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { - const uniqueItems = []; + const uniqueItemSet = new Set; - 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 (let i=0 ;i Date: Sat, 14 Feb 2026 19:19:51 +0000 Subject: [PATCH 4/4] Refactor find_common_items --- .../find_common_items/find_common_items.py | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 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..024828b 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -1,21 +1,24 @@ -from typing import List, Sequence, TypeVar +from typing import List, TypeVar ItemType = TypeVar("ItemType") +# Original version: O(n × m) due to nested loops +# Refactored version: O(n + m) using a set for constant-time lookups +# This is optimal and cannot be further reduced because all input +# elements must be processed at least once. +# use this link :https://www.w3schools.com/python/ref_set_intersection.asp def find_common_items( - first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] + first_sequence, second_sequence ) -> List[ItemType]: """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity:O(n + m) + Space Complexity:O(n + m) + Optimal time complexity:O(n + m) """ - 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 + second_set=set(second_sequence) + first_set=set(first_sequence) + common_set=first_set.intersection(second_set) + return list(common_set)