Challenge Prit’s solution (defn flipping-bits [n] "TOC: <1h Complexity: O(1), since bounded by the 32 bit limit" (let [bin-seq (seq (Long/toBinaryString n)) ;; looked up toBinaryString online size (count bin-seq) padded-bin-seq (concat (repeat (- 32 size) \0) bin-seq)] (loop [padded-bin-seq padded-bin-seq flipped-val 0] (if (empty? padded-bin-seq) flipped-val (recur (rest padded-bin-seq) (+ flipped-val (if (= (first padded-bin-seq) \0) (reduce * (repeat (dec (count padded-bin-seq)) 2)) 0))))))) (flipping-bits 4294967295)
Hackerrank Flipping bits
Hackerrank Flipping bits
Hackerrank Flipping bits
Challenge Prit’s solution (defn flipping-bits [n] "TOC: <1h Complexity: O(1), since bounded by the 32 bit limit" (let [bin-seq (seq (Long/toBinaryString n)) ;; looked up toBinaryString online size (count bin-seq) padded-bin-seq (concat (repeat (- 32 size) \0) bin-seq)] (loop [padded-bin-seq padded-bin-seq flipped-val 0] (if (empty? padded-bin-seq) flipped-val (recur (rest padded-bin-seq) (+ flipped-val (if (= (first padded-bin-seq) \0) (reduce * (repeat (dec (count padded-bin-seq)) 2)) 0))))))) (flipping-bits 4294967295)