-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestDifference.js
More file actions
30 lines (25 loc) · 1003 Bytes
/
smallestDifference.js
File metadata and controls
30 lines (25 loc) · 1003 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
// Write a function that takes in two non-empty arrays of integers.
// The function should find the pair of numbers (one from the first array, one from the second array)
// whose absolute difference is closest to zero. The function should return an array containing these two numbers,
// with the number from the first array in the first position.
// Assume that there will only be one pair of numbers with the smallest difference.
function smallestDifference(arrayOne, arrayTwo) {
arrayOne.sort((a, b) => a - b)
arrayTwo.sort((a, b) => a - b)
let smallestSoFar = Infinity, result = []
let i = 0, j = 0
while(i < arrayOne.length || j < arrayTwo.length) {
let currDiff = Math.abs(arrayOne[i] - arrayTwo[j])
if(currDiff < smallestSoFar) {
smallestSoFar = currDiff
result = [arrayOne[i], arrayTwo[j]]
}
if(arrayOne[i] < arrayTwo[j]) {
if(i < arrayOne.length - 1)
++i
} else if(j < arrayTwo.length - 1) {
++j
}
}
return result
}