Created
December 4, 2011 04:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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