O(n) Clojure solution:
(defn move-zeros [arr] (loop [lst arr nonzeros [] zeros []] (cond (empty? lst) (concat nonzeros zeros) (= (first lst) 0) (recur (rest lst) nonzeros (conj zeros 0)) :else (recur (rest lst) (conj nonzeros (first lst)) zeros)))) (move-zeros [12 3 0 2 8 11 0 0 6 4 0 5 7 0 8 9 0]) ;; (12 3 2 8 11 6 4 5 7 8 9 0 0 0 0 0 0)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
O(n) Clojure solution: