Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about this?</p> <pre><code>var arr = new [] {5, 1, -7, 3, 7}; var xs = from n in Enumerable.Range(0, arr.Length) from l in Enumerable.Range(1, arr.Length - n) let subseq = arr.Skip(n).Take(l) orderby subseq.Count() descending orderby subseq.Sum() descending select subseq; var maxSumSubseq = xs.First(); </code></pre> <p>EDIT: Added <code>orderby subseq.Count() descending</code> to get maximal length subsequence.</p> <hr> <p>EDIT: Added explanation as per comment.</p> <ol> <li><p>Select all possible subsequence starting indices:</p> <pre><code>from n in Enumerable.Range(0, arr.Length) </code></pre></li> <li><p>Select all possible lengths of subsequences given the starting index:</p> <pre><code>from l in Enumerable.Range(1, arr.Length - n) </code></pre></li> <li><p>Extract the subsequence from the array:</p> <pre><code>let subseq = arr.Skip(n).Take(l) </code></pre></li> <li><p>Order subsequences by descending length (i.e. longest first) - could order by <code>l</code> instead of <code>subseq.Count()</code> but the latter is more expressive even though the former is more efficient:</p> <pre><code>orderby subseq.Count() descending </code></pre></li> <li><p>Calculate the sum of each subsequence and order the subsequences so highest valued sums are first:</p> <pre><code>orderby subseq.Sum() descending </code></pre></li> <li><p>Select the subsequences:</p> <pre><code>select subseq; </code></pre></li> <li><p>Only select the first subsequence - it's the highest value sum with the greatest length:</p> <pre><code>xs.First(); </code></pre></li> </ol> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload