Skip to content

Instantly share code, notes, and snippets.

@exegeteio
Created January 2, 2023 12:22
Show Gist options
  • Save exegeteio/99d7618b893a87810e0139c30cb38595 to your computer and use it in GitHub Desktop.
Save exegeteio/99d7618b893a87810e0139c30cb38595 to your computer and use it in GitHub Desktop.
Cassidoo newsletter - 2023 - Week 0
# frozen_string_literal: true
def max_subarray(haystack, target_length)
cons = haystack.each_cons(target_length).to_a
sums = cons.collect(&:sum)
cons[sums.index(sums.max)]
end
puts max_subarray([-4, 2, -5, 1, 2, 3, 6, -5, 1], 4).inspect
puts max_subarray([1, 2, 0, 5], 2).inspect
puts max_subarray([10, 20, 30, 50, -10, 90], 2).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment