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,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<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 All @@ -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.
21 changes: 18 additions & 3 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
31 changes: 28 additions & 3 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -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<number>} numbers - Array of numbers to search through
* @param {number} target - Target sum to find
Expand All @@ -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)
14 changes: 11 additions & 3 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 21 additions & 7 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,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
7 changes: 4 additions & 3 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,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)):
Expand Down
7 changes: 4 additions & 3 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
Loading