Skip to content
Closed
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
@@ -1,34 +1,28 @@
/**
* Calculate the sum and product of integers in a list
* 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: O(n)
* Space Complexity: O(1)
* Optimal Time Complexity: O(n)
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* The original solution used two separate loops.
* This version combines both calculations into a single loop,
* reducing the number of iterations while keeping constant memory usage.
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
* @returns {Object} Object containing the sum 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;
}

return {
sum: sum,
product: product,
sum,
product,
};
}
28 changes: 21 additions & 7 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/**
* Finds common items between two arrays.
* Finds unique common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n + m)
* Space Complexity: O(n + k)
* Optimal Time Complexity: O(n + m)
*
* The original solution used Array.includes(), which searches
* the second array for every element in the first array.
* This version converts the second array into a Set so lookups
* are much faster.
*
* @param {Array} firstArray - First array to compare
* @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 findCommonItems = (firstArray, secondArray) => {
const secondSet = new Set(secondArray);
const commonItems = new Set();

for (const item of firstArray) {
if (secondSet.has(item)) {
commonItems.add(item);
}
}

return [...commonItems];
};
56 changes: 47 additions & 9 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
/**
* 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)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* The original solution used nested loops, resulting in O(n²) time.
* This version uses a Set to store previously seen numbers,
* allowing constant-time lookups.
*
* @param {Array<number>} 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) {
const seen = new Set();

for (const number of numbers) {
const complement = target - number;

if (seen.has(complement)) {
return true;
}

seen.add(number);
}

return false;
}/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* The original solution used nested loops, resulting in O(n²) time.
* This version uses a Set to store previously seen numbers,
* allowing constant-time lookups.
*
* @param {Array<number>} 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;
}
const seen = new Set();

for (const number of numbers) {
const complement = target - number;

if (seen.has(complement)) {
return true;
}

seen.add(number);
}

return false;
}
}
34 changes: 13 additions & 21 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
/**
* 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)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n)
*
* Refactor:
* The original implementation used nested loops to check for duplicates,
* resulting in O(n²) time complexity. Using a Set allows constant-time
* lookups while preserving the insertion order of unique values.
*
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ 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}
Time Complexity: O(n)
Space Complexity: O(1)
Optimal Time Complexity: O(n)

Refactor:
The original implementation iterated through the list twice:
once to calculate the sum and once to calculate the product.
This refactor combines both calculations into a single loop,
reducing the number of iterations while maintaining O(n)
time complexity.

sum = 0
for current_number in input_numbers:
sum += current_number
Args:
input_numbers: List of integers.

Returns:
Dictionary containing the sum and product.
"""

total_sum = 0
product = 1
for current_number in input_numbers:
product *= current_number

return {"sum": sum, "product": product}
for number in input_numbers:
total_sum += number
product *= number

return {
"sum": total_sum,
"product": product,
}
Loading