Skip to content

Instantly share code, notes, and snippets.

@mpereira
Last active May 19, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpereira/e2351ca53c357540e80dc554c4b01d0e to your computer and use it in GitHub Desktop.
Save mpereira/e2351ca53c357540e80dc554c4b01d0e to your computer and use it in GitHub Desktop.
(defn find-pairs-sum-equal-k [xs k]
(let [x-indices (into {} (map-indexed (fn [idx x] [x idx]) xs))]
(keep (fn [[x idx]]
(if-let [x-complement-idx (get x-indices (- k x))]
(when (not= idx x-complement-idx)
[x (- k x)])))
x-indices)))
(find-pairs-sum-equal-k (range 0 20) 12)
;; => ([0 12] [7 5] [1 11] [4 8] [3 9] [12 0] [2 10] [11 1] [9 3] [5 7] [10 2] [8 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment