Hackerrank max array sum (incomplete)
Challenge
Solution
I’m figuring out how to get a list of all the non adjacent in an order such that the number of elements increases, and the starting index increases too.
(defn max-subset-sum [arr]
(let [index-sum (memoize
(fn [& args]
(keep-indexed #(if (.contains args %1) %2) arr)
))]
(for [start-index (range (count arr))
range-size (range 1 (count arr))
]
(->>
(range start-index (min (count arr)
(+ range-size start-index)))
(filter (fn [index] (= (mod index 2) 0)))
)
#_(map (fn [mod-size]
(->>
(range start-index (min (count arr)
(+ range-size start-index)))
(filter (fn [index] (= (mod index mod-size) 0)))
))
(range 2 (count arr)))
#_(flatten
(partition 1 partition-size (range start-index (min (count arr)
(+ range-size start-index)))))
#_[subset-size start-index]
#_[[i j]
[i (count arr)]
(keep-indexed #(if (and (>= j i) (= (mod %1 j) 0)) %2 nil) (range i (count arr)))]
#_(let [indices (range i (count arr))]
(apply index-sum (keep-indexed #(if (= (mod %1 j) 0) %2 nil) indices))
))))
(max-subset-sum [-2 1 3 -4 5])