Hackerrank minimum absolute distance
Challenge
Solution
I first sort the array in O(n log n), and then I loop through two successive elements of the array and calculate the absolute difference, then I take the min of the resulting array.
(defn minimum-absolute-difference
"TOC: <40min
Complexity: O(n log n), (= (count arr) n)"
[arr]
(->>
(loop [arr-rem (sort arr) result []]
(if-not (second arr-rem)
result
(recur (rest arr-rem)
(conj result (java.lang.Math/abs (- (first arr-rem)
(second arr-rem)))))))
(apply min)))
(minimum-absolute-difference [-59 -36 -13 1 -53 -92 -2 -96 -54 75])
(minimum-absolute-difference [1 -3 71 68 17])