-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumZero.js
More file actions
48 lines (41 loc) · 1.09 KB
/
sumZero.js
File metadata and controls
48 lines (41 loc) · 1.09 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
44
45
46
47
48
/*
Write a function called sumZero which accepts a sorted array of integers.
The function should find the first pair where the sum is 0.
Return an array that includes both value that sums to zero or undefined if a pair does not exist
*/
//// SOLUTION 1
//// TIME: O(N^2)
// function sumZero(arr) {
// for (let i = 0; i < arr.length; i++) {
// for (let j = i + 1; j < arr.length; j++) {
// if (arr[i] + arr[j] === 0) {
// return [arr[i], arr[j]];
// }
// }
// }
// }
// console.log("solution 1");
// console.log(sumZero([-2, 0, 1, 2, 3]));
// console.log(sumZero([-4, -3, -2, -1, 0, 1, 2, 3, 5]));
// console.log(sumZero([-2, 0, 1, 3, 4]));
//// SOLUTION 2
//// TIME: O(N)
function sumZero(arr) {
let i = 0;
let j = arr.length - 1;
let sum;
while (i < j) {
sum = arr[i] + arr[j];
if (sum == 0) {
return [arr[i], arr[j]];
} else if (sum > 0) {
j--
} else {
i++
}
}
}
console.log('solution 2');
console.log(sumZero([-2, 0, 1, 2, 3]));
console.log(sumZero([-4, -3, -2, -1, 0, 1, 2, 3, 5]));
console.log(sumZero([-2, 0, 1, 3, 4]));