Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "two passes == 2n operations" and "1 pass == n operation"?
What are these operations? Does that mean one pass is guaranteed to take half as much time to complete?

The code still needs to perform sum += num and product *= num whether we use one loop or two loops.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I said “2n vs n operations”, I meant loop iterations. However, both implementations still perform n additions and n multiplications. The single-loop version reduces loop overhead and improves cache efficiency, but asymptotically both are O(n). One pass is not guaranteed to take half the time, because Big-O ignores constant factors and the arithmetic work remains the same.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a good understanding and the performance is indeed improved when two loops are reduced to one.
However, the improvement is not really "doubled", so in complexity analysis we don't focus on the constant factor.

*
* @param {Array<number>} 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;
}

Expand Down
32 changes: 11 additions & 21 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
16 changes: 7 additions & 9 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean solution!



29 changes: 21 additions & 8 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading