Nearest Pair
March 13, 2018
Our solution first stores each value in the list with its position, then iterates through the input list, looking up the position of the conjugate and saving the pair if it is nearer than the current best solution:
(define (nearest-pair xs target)
(let ((pos (list)))
(do ((i 0 (+ i 1)) (ys xs (cdr ys))) ((null? ys))
(set! pos (cons (cons (car ys) i) pos)))
(let loop ((i 0) (xs xs) (np #f) (dist #e1e9))
(if (null? xs) np
(let ((j (assoc (- target (car xs)) pos)))
(cond ((not j) (loop (+ i 1) (cdr xs) np dist))
((= i (cdr j)) (loop (+ i 1) (cdr xs) np dist))
((< (abs (- i (cdr j))) dist)
(loop (+ i 1) (cdr xs) (cons (car xs) (car j)) (abs (- i (cdr j)))))
(else (loop (+ i 1) (cdr xs) np dist))))))))
For no particular reason, we use association lists rather than hash tables to store the pos dictionary. There are several important quantities with similar definitions: (- target (car xs)) is the number that must be added to the current item to make the desired total, (car j) is that number, and (cdr j) is the position of that number, if it exists in the input list. (abs (- i (cdr j))) is the distance between the current item and its conjugate. It’s easy to get those confused; I did while writing this program.
You car run the program at https://ideone.com/5t2k65.
var a = [1, 5, 3, 6, 4, 2];
var p = [];
var q = [];
for(i=0;i<a.length;i++){
for(j=i+1;j<a.length;j++){
if(a[i] + a[j] === 7){
var k = j – i;
p.push([k]);
q.push([k,[a[i],a[j]]]);
}
This being an interview question, you’d definitely want to ask if you can assume the numbers in the list are unique.
is second iteration necessary ” for(s=0;s<item.length;s++){
A Haskell version.
import Data.Function (on) import Data.List (minimumBy, tails) -- A position and value. data PV a b = PV { pos :: a, val :: b } -- The nearest pairs of values from xs that sum to s, or nothing if no values -- have that sum. nearestPair :: Real a => a -> [a] -> Maybe (a, a) nearestPair s xs = let ps = filter (sumTo s) $ pairs $ mkPVs xs in case ps of [] -> Nothing _ -> Just $ both val $ minimumBy (compare `on` dist) ps -- The equivalent list of PVs starting at position 0. mkPVs :: [a] -> [PV Int a] mkPVs = zipWith PV [0..] -- All pairs of elements from xs, where the first element appears strictly -- earlier in the argument list than the second. pairs :: [a] -> [(a, a)] pairs xs = [(x1, x2) | (x1:xs') <- tails xs, x2 <- xs'] -- True iff the values sum to s. sumTo :: (Num b, Eq b) => b -> (PV a b, PV a b) -> Bool sumTo s (x, y) = val x + val y == s -- The distance, by position, between two PVs. dist :: Num a => (PV a b, PV a b) -> a dist (x, y) = abs (pos x - pos y) -- The result of applying the function to both elements of the tuple. both :: (a -> b) -> (a, a) -> (b, b) both f (x, y) = (f x, f y) main :: IO () main = do print $ nearestPair 7 [1, 5, 3, 6, 4, 2] print $ nearestPair 2 [1, 5, 3, 6, 4, 2]it can be done in O(N) using hash tables
Here’s a solution in Python.
from collections import defaultdict # Returns the indices of the nearest pair. # (returns indices to distinguish duplicated values) def find_nearest_pair(l, target): index_lookup = defaultdict(list) for idx, x in enumerate(l): index_lookup[x].append(idx) nearest_pair = None nearest_distance = None for idx1, x in enumerate(l): y = target - x if y not in index_lookup: continue for idx2 in index_lookup[y]: if idx1 >= idx2: continue distance = idx2 - idx1 if (nearest_pair is None) or (distance < nearest_distance): nearest_pair = (idx1, idx2) nearest_distance = distance return nearest_pair l = [1, 5, 3, 6, 4, 2] print 'list: {}'.format(l) print for target in range(13): print 'target: {}'.format(target) nearest_pair = find_nearest_pair(l, target) if not nearest_pair: continue print " indices: {}".format(nearest_pair) print " values: {}".format(tuple(l[idx] for idx in nearest_pair))Output:
Line 13 can be removed from my prior solution, as the defaultdict will return an empty list.
O(n) solution:
def nearest_pair(iterable, target): best_pair = None best_span = None seen = {} for index, right_no in enumerate(iterable): left_no = target - right_no if left_no in seen: span = abs(index - seen[left_no]) if best_span is None or span < best_span: best_span = span best_pair = (left_no, right_no) seen[right_no] = index return best_pair