-
Notifications
You must be signed in to change notification settings - Fork 1
/
closestPair.js
65 lines (57 loc) · 1.95 KB
/
closestPair.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Find the closest pair from two sorted arrays from https://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/
/*
Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array.
We are given two arrays ar1[0…m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j] such that absolute value of (ar1[i] + ar2[j] – x) is minimum.
Example:
Input: ar1[] = {1, 4, 5, 7};
ar2[] = {10, 20, 30, 40};
x = 32
Output: 1 and 30
Input: ar1[] = {1, 4, 5, 7};
ar2[] = {10, 20, 30, 40};
x = 50
Output: 7 and 40
*/
// O(m + n) as we are looping through both arrays once
// O(1) constant extra space
const findClosestPair = (arr1, arr2, x) => {
// initialize minimum difference as Infinity and result (undefined)
let minDiff = Infinity;
let result;
// initialize l as first index of arr1 (0)
let l = 0;
// initialize r as last index of arr2
let r = arr2.length - 1;
// loop until reaching the end of either arr
while (l < arr1.length && r > -1) {
// sum arr1[l] & arr2[r]
const sum = arr1[l] + arr2[r];
// set diff to |sum - x|
const diff = Math.abs(sum - x);
// if diff is less than previous minDiff, update minDiff and result
if (diff < minDiff) {
minDiff = diff;
result = [arr1[l], arr2[r]];
} else if (sum < x) {
// if sum is < x, increment l to get a larger sum
l++;
} else {
// otherwise, sum is > x; decrement r to get a smaller sum
r--;
}
}
return result;
};
const {expect} = require('chai');
describe('findClosestPair function', () => {
const arr1 = [1, 4, 5, 7];
const arr2 = [10, 20, 30, 40];
it('should return [1, 30]', () => {
const x = 32;
expect(findClosestPair(arr1, arr2, x)).to.eql([1, 30]);
});
it('should return [7, 40]', () => {
const x = 50;
expect(findClosestPair(arr1, arr2, x)).to.eql([7, 40]);
});
});