title: Ruby - Finding the Maximum Difference among subarrays
body: <p>I came across a question from a coding challenge site that deals with subsequences.</p>
  
  <p>Given an array of integer elements, a subsequence of this array is a set of consecutive elements from the array (i.e: given the array v: [7, 8, -3, 5, -1], a subsequence of v is 8, -3, 5)</p>
  
  <p>Your task is to write a function that finds a left and a right subsequence of the array that satisfy the following conditions:</p>
  
  <ol>
  <li><p>Two subsequences must be unique (they don’t have shared elements)</p></li>
  <li><p>The difference between the sum of the elements in the right subsequence and the sum of the elements in the left subsequence is maximum</p></li>
  <li><p>Print the difference to the standard output (stdout)</p></li>
  </ol>
  
  <p>The function receives <strong>array</strong>, which is an array of integers.</p>
  
  <p>Data constraints:</p>
  
  <ol>
  <li><p>the array has at least 2 and at most 1,000,000 numbers</p></li>
  <li><p>all the elements in the array are integer numbers in the following range: [-1000, 1000]</p></li>
  </ol>
  
  <p>Example</p>
  
  <pre><code>Input: 3, -5, 1, -2, 8, -2, 3, -2, 1
  Output: 15
  </code></pre>
  
  <p>In the above example, the left subsequence is [-5, 1, -2] and the right subsequence is [8,-2,3]</p>
  
  <pre><code>(8 + -2 + 3) - (-5 + 1 + -2) = 9 - -6 = 15
  </code></pre>
  
  <p>Here's what I've tried</p>
  
  <pre><code>def maximum_difference(array)
    sequences = array.each_cons(3).to_a
    pairs = sequences.each_cons(2).to_a
    pairs.select{|pair|pair[0]+pair[1].length != pair[0] + pair[1].uniq.length}
    pairs.map{|values|values.reduce(:+)}.sort{|max, min| max - min}
  end
  </code></pre>
  
  <p>but it looks like that doesn't work.</p>