Practice the most popular front-end questions asked in coding interviews with Frontend Churn

00:00:00

Given two arrays of integers, find the pair of values (one value in each array) with the smallest (non-negative) difference.

Note: Pick only one element from each of the arrays.

```
arr1 = [-1, 5, 10, 20, 28, 3]
arr2 = [26, 134, 135, 15, 17]
```

```
[ 28, 26 ]
```

The smallest difference between elements from the first array and elements from the second array is = `2`

. This comes when 5th element from arr1 i.e. `28`

is subtracted from the first element from arr2 i.e. `26`

. Hence, the answer is `[28, 26]`

```
arr1 = [1, 2, 3, 4, 5, 6]
arr2 = [12, 13, 14, 15, 16, 17]
```

```
[ 6, 12 ]
```

The smallest difference will be `12 - 6`

= 6. Hence, `[6, 12]`

is returned.

```
-10^6 <= arr1[i] <= 10^6
-10^6 <= arr2[i] <= 10^6
1 <= arr1.length <= 1000
1 <= arr2.length <= 1000
```

Click to reveal

Can sorting help you with this problem?

Click to reveal

Can you try iterating over the SORTED arrays and find some pattern? Can you use two pointer approach?

Uploaded by: Manu Arora

Share this problem on