-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckingSum.js
44 lines (36 loc) · 1.04 KB
/
CheckingSum.js
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
// function getSumPairZero(array) {
// for (let number of array) {
// // console.log(number);
// for (let j = 1; j < array.length; j++) {
// if (number + array[j] === 0) {
// return [number, array[j]];
// }
// }
// }
// }
// const result = getSumPairZero([-5, -4, -3, -2, 0, 2, 4, 6, 8]);
// console.log(result);
// 0(n^2) quadratic time complexity
// code for better time complexity - linear
function getSumPairZero(array) {
let left = 0;
let right = array.length - 1;
while (left < right) {
sum = array[left] + array[right];
if (sum === 0) {
return [array[left], array[right]];
} else if (sum > 0) {
right--;
} else {
left++;
}
}
}
const result = getSumPairZero([-5, -4, -3, -2, 0, 2, 4, 6, 8]);
console.log(result);
// o(n) linear complexity
// take left 0 index value
//take right 0 index value
//add both numbers if greater then 0 then shift right index amd continue loop
//if less then 0 then shift left index value and same
// If equal to 0 then return value [?, ?]