-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized.ins.js
More file actions
30 lines (25 loc) · 910 Bytes
/
optimized.ins.js
File metadata and controls
30 lines (25 loc) · 910 Bytes
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
function smallestDifference(arrayOne, arrayTwo) {
arrayOne.sort((a,b) => a-b);
arrayTwo.sort((a,b) => a-b);
let p1 = 0, p2 = 0, currentDifference;
let currentMinimumClosestToZero = Number.MAX_VALUE;
let currentPair = [];
while (p1 < arrayOne.length && p2 < arrayTwo.length){
if (arrayOne[p1] === arrayTwo[p2]) return [arrayOne[p1], arrayTwo[p2]]
let digit1 = arrayOne[p1];
let digit2 = arrayTwo[p2];
if (digit1 > digit2){
p2++;
}else{
p1++;
}
currentDifference = Math.abs(digit1 - digit2);
if (currentDifference < currentMinimumClosestToZero){
currentMinimumClosestToZero = currentDifference;
currentPair = [digit1, digit2];
}
}
return currentPair;
}
console.log(smallestDifference([-1, 5, 10, 20, 28, 3], [26, 134, 135, 15, 17]));
console.log(smallestDifference([10, 1000], [-1441, -124, -25, 1014, 1500, 660, 410, 245, 530]));