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,26 +9,27 @@
* "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) {
product *= num;
sum += num;
product *= num
}

return {
sum: sum,
product: product,
};
}
//Time Complexity: O(n) The loop visits each element exactly once
// Space complexity is O(1) he function uses a fixed number of variables, regardless of the input size.
// The returned object also has a fixed size.
// Optimal time complexity is O(n) because every element must be examined
// to guarantee the correct sum and product.
32 changes: 25 additions & 7 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
*
* Time Complexity: O(n * m)
* Space Complexity: O(n)
* Optimal Time Complexity: O(n + m)
*
* @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 allowedItems = new Set(secondArray);

const uniqueValues = [
...new Set(firstArray.filter((item) => allowedItems.has(item))),
];
return uniqueValues;
};

console.log(findCommonItems([2, 5, 7], [5, 6, 7]));


// Time Complexity: O(n + m) on average.
// Building the Set takes O(m), and filtering the first array takes O(n)
// on average because Set.has() provides efficient lookups.

// Space Complexity: O(n + m).
// The Set stores elements from secondArray, while the filter result
// and the Set of unique common items require additional space.

// Improvement: Reduces average time complexity from O(n * m) to O(n + m).
23 changes: 17 additions & 6 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
*
* Time Complexity: O(N^2)
// Space Complexity: O(1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the space complexity O(1) both before and after your change?

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.

Is the space complexity O(1) both before and after your change?

The original uses O(1) space and O(n²) time. The updated version uses O(n) space but improves the time to O(n) on average by using a Set for faster lookups.

// Optimal Time Complexity: O(N)
*
* @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 seenNumbers = new Set();

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

if (seenNumbers.has(complement)) return true;

seenNumbers.add(currentNumber);
}
return false;

}


// Time Complexity: O(n) We check each number once.
// Space Complexity: O(n) The Set may store all n numbers.
37 changes: 15 additions & 22 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@
* @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 [...new Set(inputSequence)]

return uniqueItems;
}
// Original complexity:
// The original approach typically uses nested loops to compare
// each value with the remaining values in the sequence.
// This results in O(n²) time complexity because each value
// may be compared with every other value.


// Refactor:
// Use a Set to remove duplicate values while preserving the
// order of their first occurrence.
// The Set stores only unique values, and spreading the Set
// into a new array creates the result.
// Set insertion is O(1) on average, so processing all n
// elements takes O(n) time overall.
Loading