Finding if sequential numbers for total exists
Your code incorrectly returns false if I give it [23, 5, 4, 7, 2, 11, 1] and a target value of 20. The answer should still be 7 + 2 + 11, but it doesn't find it.

That's because the reduce operation always runs to the end of the array, so it is unable to pick out a matching sub-sequence in the middle of a larger sequence. It can only find it, if it's at the end. I.e. it does this:

5 + 4 + 7 + 2 + 11 + 1 =&gt; 30
4 + 7 + 2 + 11 + 1     =&gt; 25
7 + 2 + 11 + 1         =&gt; 21
2 + 11 + 1             =&gt; 13 # could just give up here, really
11 + 1                 =&gt; 12
1                      =&gt; 1

=&gt; false


However 2 + 7 + 11 would've been a valid answer.

Your example input just happens to be crafted so that you get correct results; the matching sub-sequences are always anchored to the end of an array. They're never in the middle of it.

Furthermore, by chunking using i &lt; max you remove values that are equal to the target sum. I.e. trying [1, 3, 7, 5, 23, 2] with a target of 7 will fail, even though there is a sequence - a sequence of 1 value - that matches.

This is however easily fixed by changing the block to use i &lt;= max.

Overall, I'd propose something like this:

def sub_sequence_equal_to?(total, values)
  sequences = values.chunk { |n| n &lt;= total || nil }.map(&amp;:last)

  sequences.each do |sequence|
    sequence.reduce(0) do |sum, n|
      sum += n
      sum -= sequence.shift while sum &gt; total
      return true if sum == total
      sum
    end
  end

  false
end


It'll gradually build a sum, but also subtract the earliest values if said sum has grown too large.

So given [23, 5, 4, 7, 2, 11, 1] and a target value of 20 it'll do something like:

5                  =&gt; 5
5 + 4              =&gt; 9
5 + 4 + 7          =&gt; 16
5 + 4 + 7 + 2      =&gt; 18
5 + 4 + 7 + 2 + 11 =&gt; 29
    4 + 7 + 2 + 11 =&gt; 24
        7 + 2 + 11 =&gt; 20

=&gt; true


It's not quite \$O(n)\$, and perhaps there's a smarter way (it'd be nice to get rid of that separate chunk step for instance). Still, it's more efficient (and works better) than the original.