Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created December 4, 2011 04:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahlz/1429203 to your computer and use it in GitHub Desktop.
Save noahlz/1429203 to your computer and use it in GitHub Desktop.
(Clojure version) Given an array of integers, determine if any two given elements sum to a given target.
(defn any-pair-make-sum? [arr target]
(let [m (frequencies arr)]
(loop [f (first arr)
x (- target f)
r (rest arr)]
(let [entry (find m x)
only-needs-one-count (not (= x f))]
(if (and entry (or only-needs-one-count (> 1 (val entry))))
true
(if (empty? r)
false
(recur (first r)
(- target (first r))
(rest r))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment