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 @@ -18,17 +18,24 @@
*/
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;
}

return {
sum: sum,
product: product,
};
}
// Time Complexity: O(n)
// The original implementation also had O(n) time complexity,
// but it iterated through the array twice (approximately 2n operations).
// The refactored version calculates both the sum and product in one loop,
// so it only traverses the array once (approximately n iterations).
//
// Space Complexity: O(1)
// We only store two variables regardless of the size of the input.
// Optimal Time Complexity: O(n)
// We must iterate through every element at least once
// to calculate both the sum and the product.
33 changes: 30 additions & 3 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@
* @param {Array} secondArray - Second array to compare
* @returns {Array} Array containing unique common items
*/
export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];
// export const findCommonItems2 = (firstArray, secondArray) => [
// ...new Set(firstArray.filter((item) => secondArray.includes(item))), //O(N*M)
// ];
export const findCommonItems = (firstArray, secondArray) => {
const secondSet = new Set(secondArray); //O(M)
const commonSet = new Set();

for (const item of firstArray) {
//O(N)
if (secondSet.has(item)) {
commonSet.add(item);
}
}

return [...commonSet]; //O(K)
};
// Time Complexity: O(n + m)
// We create a Set from secondArray and loop through firstArray once.

// Original Time Complexity: O(n * m)
// includes() may search all of secondArray for every item in firstArray.

// Space Complexity: O(m + n)
// We store secondArray in a Set and up to n common items.

// Original Space Complexity: O(n)
// filter() and Set create temporary collections of up to n items.

// Optimal Time Complexity: O(n + m)
// We need to inspect both arrays to find all common items.
37 changes: 31 additions & 6 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,38 @@
* @param {number} target - Target sum to find
* @returns {boolean} True if pair exists, false otherwise
*/
// export function hasPairWithSum2(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;
// }
// }
// }
// return false;
// }
// Time Complexity: O(N^2)
// Space Complexity: O(1)
// Optimal Time Complexity: O(N)

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;
}
}
const seen = new Set();
for (const num of numbers) {
const needed = target - num;
if (seen.has(needed)) return true;

seen.add(num);
}

return false;
}
// Original Time Complexity: O(n^2)
//two nested loop compare each number with remaining numbers

// Time Complexity: O(n)
// we iterate through the arr once and Set.has() for O(1) lookup

// Space Complexity: O(n)
// We may store up to n numbers in the Set.
// Optimal Time Complexity: O(n)
//we might need to inspect every number at least once.
65 changes: 41 additions & 24 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,46 @@
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
// export function removeDuplicates(inputSequence) {
// 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]);
// }
// }

// return uniqueItems;
// }

export function removeDuplicates(inputSequence) {
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]);
}
}

return uniqueItems;

return [...new Set(inputSequence)]; //O(N+N)
}

// Original Time Complexity: O(n * k), worst case O(n^2)
// For each item, the code may scan all uniqueItems to check for duplicates.

// Time Complexity: O(n)
// Creating the Set processes each item once, and spreading it back to an array is also O(n).

// Space Complexity: O(n)
// The Set and returned array can store up to n unique items.

// Optimal Time Complexity: O(n)
// We must inspect every element at least once to remove duplicates.
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
from typing import Dict, List

# def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
# """
# Calculate the sum and product of integers in a list.

def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
"""
Calculate the sum and product of integers in a list.
# Note: the sum is every number added together
# and the product is every number multiplied together
# so for example: [2, 3, 5] would return
# {
# "sum": 10, // 2 + 3 + 5
# "product": 30 // 2 * 3 * 5
# }
# Time Complexity:
# Space Complexity:
# Optimal time complexity:
# """
# # Edge case: empty list
# if not input_numbers:
# return {"sum": 0, "product": 1}

# sum = 0
# for current_number in input_numbers:
# sum += current_number

Note: the sum is every number added together
and the product is every number multiplied together
so for example: [2, 3, 5] would return
{
"sum": 10, // 2 + 3 + 5
"product": 30 // 2 * 3 * 5
}
Time Complexity:
Space Complexity:
Optimal time complexity:
"""
# Edge case: empty list
if not input_numbers:
return {"sum": 0, "product": 1}
# product = 1
# for current_number in input_numbers:
# product *= current_number

sum = 0
for current_number in input_numbers:
sum += current_number

product = 1
for current_number in input_numbers:
product *= current_number
# return {"sum": sum, "product": product}
def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
total: int = 0
product: int = 1
for num in input_numbers:
total += num
product *= num

return {"sum": sum, "product": product}
return {"sum": total, "product": product}
38 changes: 25 additions & 13 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
ItemType = TypeVar("ItemType")


# def find_common_items(
# first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]
# ) -> List[ItemType]:
# """
# Find common items between two arrays.

# Time Complexity:
# Space Complexity:
# Optimal time complexity:
# """
# 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
Comment on lines +16 to +21

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's the complexity of the original code? (The Python implementation is different from the JS implementation)

@AhmadHmedann AhmadHmedann Oct 5, 2026 •

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.

original
Time Complexity: O(n * m * k), worst case O(n^3)
Two nested loops compare the sequences, and checking
"i not in common_items" can require scanning the result list.

new
Time Complexity : O(n+m)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why enlarged the text? It looks like 'yelling".

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.

Sorry about that. I didn’t realise that adding # would make the line render as a large heading. It looked small while I was typing it, so I didn’t notice.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I know you didn't meant it.

I always use the "Preview" feature to check what I typed on GitHub.

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.

Thank you for the feedback. From now on, I’ll use the Preview feature to check my comments before posting them.



def find_common_items(
first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]
) -> List[ItemType]:
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
"""
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
first_set = set(first_sequence)
second_set = set(second_sequence)

common = first_set.intersection(second_set)
# common = first_set & second_set

return list(common)
31 changes: 21 additions & 10 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 @@ -3,16 +3,27 @@
Number = TypeVar("Number", int, float)


# 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:
# """
# 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

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.
seen = set()
for num in numbers:
needed = target_sum - num
if needed in seen:
return True

seen.add(num)

Time Complexity:
Space Complexity:
Optimal time complexity:
"""
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
51 changes: 31 additions & 20 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
ItemType = TypeVar("ItemType")


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:
"""
unique_items = []

for value in values:
is_duplicate = False
for existing in unique_items:
if value == existing:
is_duplicate = True
break
if not is_duplicate:
unique_items.append(value)

return unique_items
# 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:
# """
# unique_items = []

# for value in values:
# is_duplicate = False
# for existing in unique_items:
# if value == existing:
# is_duplicate = True
# break
# if not is_duplicate:
# unique_items.append(value)

# return unique_items

def remove_duplicates(values):
seen = set()
result = []

for item in values:
if item not in seen:
seen.add(item)
result.append(item)

return result
Loading