Solutions
1. Brute Force
O(n²logn) time | O(n²) space
Intuition
We can take the distance of every pair, sort all of them and return the k-th element.
Algorithm
- Create an empty array for the distances.
- Iterate through the outer loop from the first element to the last element with
iindex. - For each outer loop iteration, iterate through the inner loop from the element at
i + 1to the last element withjindex, and add the distance between the two elements to the array. - Sort the array.
- Return the element at index
k - 1.
Code
2. Heap
O(nlogn + klogn) time | O(n) space
Intuition
If we sort the array first, the smallest distance will be between some of two neighbours. The second smallest distance is either between another two neighbours, or one element to the right of the smallest pair.
If we put every distance in a table with a row for each i index and a column for each j index for the sorted array [1, 1, 4, 6], the smallest distance left in a row is always the leftmost one we have not taken yet.
| j = 1 | j = 2 | j = 3 | |
|---|---|---|---|
| i = 0 | 0 | 3 | 5 |
| i = 1 | 3 | 5 | |
| i = 2 | 2 |
If we keep a heap of only the leftmost distance of every row, the k-th distance we take from the heap is the answer.
Algorithm
- Sort the array.
- Create a min heap with the distance of every pair of neighbours and the two indexes it was measured between.
- Iterate through the loop
k - 1times.- Pop the smallest pair from the heap.
- Push the distance between the element at the same first index and the element after the second index.
- Return the distance on top of the heap.
Code
3. Binary Search with Sliding Window
O(nlogn + nlogd) time | O(1) space, for the greatest distance d
Intuition
We can use the binary search technique, starting from the average of the smallest and the greatest distance instead of checking every distance.
At each step we count how many pairs have a distance no greater than that average.
When there are k or more of them, the k-th smallest distance is somewhere between the smallest distance and the average.
Otherwise it is greater than the average, so the answer is between the average and the greatest distance.
To count the pairs we use a sliding window technique on a sorted array.
We keep a window between the left and right indexes, and extend it by moving right one element forward.
When the distance of the two elements is greater than the average, we move left forward until it is not.
When we move right forward, left never goes back towards the start, because the elements only grow, so every window that starts before it has a distance greater than the average.
Then at every right index the window is every element whose distance to the one at right is no greater than the average, so it adds right - left pairs to the count.
Algorithm
- Sort the array.
- Create
lowandhighinteger variables with0and the distance between the last and the first element as initial values. - While
lowis less thanhigh, take the middle oflowandhighas the average.- Count the pairs with a distance no greater than the average: walk the array with
rightindex, moveleftforward while the distance between the elements atleftandrightis greater than the average, and addright - leftto the count on each step. - Set
highto the average when the count iskor greater, otherwise setlowto the average plus one.
- Count the pairs with a distance no greater than the average: walk the array with
- Return
low.
Code
def smallestDistancePair(nums: list[int], k: int) -> int: pass Press Visualise to step through your code.