Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Jon Skeet's / Timwi's solutions are the way to go.</p> <p>For fun, here's a LINQ query that does the job (<em>very</em> inefficiently):</p> <pre><code>var sequences = input.Distinct() .GroupBy(num =&gt; Enumerable.Range(num, int.MaxValue - num + 1) .TakeWhile(input.Contains) .Last()) //use the last member of the consecutive sequence as the key .Where(seq =&gt; seq.Count() &gt;= 3) .Select(seq =&gt; seq.OrderBy(num =&gt; num)); // not necessary unless ordering is desirable inside each sequence. </code></pre> <p>The query's performance can be improved slightly by loading the input into a <code>HashSet</code> (to improve <code>Contains</code>), but that will still not produce a solution that is anywhere close to efficient.</p> <p>The only bug I am aware of is the possibility of an arithmetic overflow if the sequence contains negative numbers <s>of large magnitude</s> (we cannot represent the <code>count</code> parameter for <code>Range</code>). This would be easy to fix with a custom <code>static IEnumerable&lt;int&gt; To(this int start, int end)</code> extension-method. If anyone can think of any other simple technique of dodging the overflow, please let me know.</p> <p>EDIT: Here's a slightly more verbose (but equally inefficient) variant without the overflow issue.</p> <pre><code>var sequences = input.GroupBy(num =&gt; input.Where(candidate =&gt; candidate &gt;= num) .OrderBy(candidate =&gt; candidate) .TakeWhile((candidate, index) =&gt; candidate == num + index) .Last()) .Where(seq =&gt; seq.Count() &gt;= 3) .Select(seq =&gt; seq.OrderBy(num =&gt; num)); </code></pre>
 

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