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


Pages: 1 2

5 Responses to “Nearest Pair”

  1. Rutger said
    l = [1,5,3,6,4,2]
    target = 7
    print(min([(d2-d1, (l[d1],l[d2])) for d1 in range(len(l)) for d2 in range(d1+1, len(l)) if l[d1]+l[d2] == target])[1])
  2. Azi said

    var a = [1, 5, 3, 6, 4, 2];
    var p = [];
    var q = [];
    if(a[i] + a[j] === 7){
    var k = j – i;

        var min = Math.min.apply(Math, p);
  3. Scott McMaster said

    This being an interview question, you’d definitely want to ask if you can assume the numbers in the list are unique.

  4. harish said

    is second iteration necessary ” for(s=0;s<item.length;s++){

            }" , as we know that  there is only two items in inner array so it could be  "  q.forEach(function(item,index){
  5. Globules said

    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]
    $ ./nearpair
    Just (3,4)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: