-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathhasPairWithSum.js
More file actions
43 lines (40 loc) · 1.55 KB
/
hasPairWithSum.js
File metadata and controls
43 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time 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;
// }
// }
// }
// return false;
// }
//My Analysis Result
// Time Complexity - since it have two nested loop of the same size/growth here i have a growth of O(n * n) = O(n**2) .. quadratic growth
// Space Complexity - I didnt see any assigning/modifying stored data scenario so i think the space complexity is just O(1) for the return statement
//The inefficiency of this program is due to the nested loop : it creates redudndant checks to compare the numbers.
//here is the refactored code avoiding that redundancy
export function hasPairWithSum(numbers, target) {
const seenNumbers = new Set();
for (const num of numbers) {
const complement = target - num;
if (seenNumbers.has(complement)) {
return true; // We found a pair!
}
seenNumbers.add(num);
}
return false;
}
//here time complexity is reduced to optimal level . just one loop which is O(n)
//for the space complexity here i have O(n) as well because of the Set() i used.
//i traded space to have an optimal time complexity