diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..b70cfa58 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,21 +9,19 @@ * "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 */ 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; } @@ -32,3 +30,9 @@ export function calculateSumAndProduct(numbers) { product: product, }; } + +// Time complexity is O(n) because we visit each element once. +// Space complexity is O(1) because we use a fixed number of variables, +// regardless of the length of the input array. +// Optimal time complexity is O(n) because every element must be examined +// to guarantee the correct sum and product. \ No newline at end of file diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..1bfedae3 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,9 +1,9 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(nm) + * Space Complexity:O(n+m) + * Optimal Time Complexity: 0(n+m) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare @@ -12,3 +12,18 @@ export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), ]; + +// Suppose that firstArray has n items and secondArray has m items. +// filter() goes through every item in firstArray → O(n) iterations. +// includes() may have to search through the entire secondArray → O(m). +// so n items × m-item search = O(nm) for time complexity +// Due to that the array produced by filter() and the final array created +// by [...new Set(...)]. These all can grow with input size. So space complexity is O(n+m) +// However,we don't necessarily need to search through secondArray from scratch for every +// item. We could turn secondArray into a Set first: +// export const findCommonItems = (firstArray, secondArray) => { +// const secondSet = new Set(secondArray); +// return [...new Set( +// firstArray.filter(item => secondSet.has(item)) +// )]; +// }; which would create Set: O(m) and search n items: O(n) so total size: O(n + m) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..f0c8cd08 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,9 +1,9 @@ /** * 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) + * Space Complexity:O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find @@ -19,3 +19,28 @@ export function hasPairWithSum(numbers, target) { } return false; } + +// Since function is to loop inside another loop, it is roughly n × n comparisons, so +// time complexity = O(n²) +// Because we do not create another array, object or set that grows with n, space +// complexity should be O(1) +// Optimal means the best complexity we can achieve with a reasonable algorithm for the problem. +// We can use a Set to remember numbers we've already seen for this function. +export function hasPairWithSum(numbers, target) { + const seen = new Set(); + + for (const number of numbers) { + const needed = target - number; + + if (seen.has(needed)) { + return true; + } + + seen.add(number); + } + + return false; +} +// Instead of checking every possible pair, we ask: +// "Have I already seen the number that would make this number equal the target?" +// which makes optimal time complexity O(n) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..0827f049 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,9 +1,9 @@ /** * 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) + * Space Complexity:O(n) + * Optimal Time Complexity:O(n) * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed @@ -34,3 +34,11 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } + +// While function shows loop inside another loop(nested loop), time complexity is O(n^2) +// Because uniqueItems is extra storage, space complexity is O(n) +// Again, we can use Set to make another function which makes faster to run +export function removeDuplicates(inputSequence) { + return [...new Set(inputSequence)]; +} +// So optimal time complexity should be O(n) diff --git a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py index cfd5cfdf..6ab11570 100644 --- a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py +++ b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py @@ -12,9 +12,10 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "sum": 10, // 2 + 3 + 5 "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) + which is the same as JavaScript function calculateSumAndProduct(numbers) """ # Edge case: empty list if not input_numbers: 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 478e2efc..5b1fb4c7 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,27 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Approach: + - Convert the second sequence to a set for faster membership checks. + - Use a set to track items already added and avoid duplicates. + - Preserve the order of items as they appear in the first sequence. + + Time Complexity: O(n + m) on average, because set lookups take O(1) + on average and we iterate through both sequences. + + Space Complexity: O(n + m) as an upper bound for the sets and output. + More precisely, O(m + k), where k is the number of unique common items. + + Optimal Time Complexity: O(n + m) on average, assuming hashable items + and constant-time set operations on average. """ + second_items = set(second_sequence) 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) + seen_items = set() + + for item in first_sequence: + if item in second_items and item not in seen_items: + common_items.append(item) + seen_items.add(item) + return common_items 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 fe2da517..5a2c1fd8 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,9 +7,10 @@ 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: O( n^2) + Space Complexity:O(1) + Optimal time complexity:O(n) + which is the same as JavaScript function hasPairWithSum(numbers, target) """ for i in range(len(numbers)): for j in range(i + 1, len(numbers)): diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe80..3c3676e7 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,9 +7,10 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: """ 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) + Space complexity:O(n) + Optimal time complexity:O(n) + which is the similar as JavaScript function removeDuplicates(inputSequence) """ unique_items = []