-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCC-29.js
36 lines (28 loc) · 1.08 KB
/
DCC-29.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
// Prompt:
// - Write a function called addChecker that accepts two arguments.
// - The first argument is an array containing at least two integers. The integers in the array are sorted in ascending order.
// - The second argument is an integer.
// - The addChecker function should return true if there are two integers in the array of integers (first argument) that when added together, equals the integer passed in as the second argument.
// - If there are no two integers in the array that sum up to equal the second argument, addChecker should return false.
// Hint:
// - An efficient solution can leverage the the fact that the integers in the array are sorted.
// Examples:
// addChecker( [1, 2], 3 ) // => true
// addChecker( [-3, 2], 9 ) // => false
// addChecker( [10, 15, 16, 22], 32 ) // => true
// addChecker( [10, 15, 16, 22], 19 ) // => false
const addChecker = (arr, num) => {
let i = 0;
let j = arr.length - 1;
while (i < j) {
const sum = arr[i] + arr[j];
if (sum === num) {
return true;
} else if (sum < num) {
i++;
} else {
j--;
}
}
return false;
};