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,22 +9,22 @@
* "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)
*The original implementation used two separate loops.
*This refactor combines them into a single loop to reduce
*the constant factor, while maintaining the same Big-O complexity.

* @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;
}

return {
Expand Down
29 changes: 19 additions & 10 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
*
* Time Complexity:before refactoring O(n²)- after refactoring O(n log n)
* Space Complexity:before refactoring O(1)- after refactoring O(1)
* Optimal Time Complexity:O(n log n)
* https://www.hellointerview.com/learn/code/two-pointers/overview
* The refactored solution first sorts the array, which takes O(n log n) time,
* and then uses the two-pointer technique to scan the array in a single pass (O(n)). The sorting step dominates the overall complexity.
* @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;
}
}
numbers.sort((a,b)=>a-b);
let left=0;
let right=numbers.length-1;
while (left<right) {
const current_sum=numbers[left]+numbers[right];
if (current_sum === target){
return true;}
if (current_sum<target){
left +=1;
} else {
right -=1;
}
}
return false;

}
12 changes: 12 additions & 0 deletions Sprint-1/JavaScript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 14 additions & 26 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +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:
*
* Time Complexity:before refactoring O(n²)- after refactoring O(n)
* Space Complexity:O(n)
* Optimal Time Complexity:O(n)

* The original implementation relied on nested loops to detect duplicates,
*resulting in O(n²) time complexity, which does not scale well for large inputs.
*The refactored solution uses a Set to perform duplicate checks in constant time.
*This removes the need for an inner loop and reduces the overall time complexity
*to O(n), which is optimal for this problem.
*using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
const uniqueItemSet = new Set;

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 (let i=0 ;i<inputSequence.length;i++){
uniqueItemSet.add(inputSequence[i]);
}

return uniqueItems;
return [...uniqueItemSet];
}
25 changes: 14 additions & 11 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
from typing import List, Sequence, TypeVar
from typing import List, TypeVar

ItemType = TypeVar("ItemType")

# Original version: O(n × m) due to nested loops
# Refactored version: O(n + m) using a set for constant-time lookups
# This is optimal and cannot be further reduced because all input
# elements must be processed at least once.
# use this link :https://www.w3schools.com/python/ref_set_intersection.asp

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

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity:O(n + m)
Space Complexity:O(n + m)
Optimal time complexity:O(n + m)
"""
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
second_set=set(second_sequence)
first_set=set(first_sequence)
common_set=first_set.intersection(second_set)
return list(common_set)