Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPD2:</strong> The following solution is for a problem when it is not required that subarray is contiguous. I misunderstood the problem statement. Not deleting this, as somebody may have an idea based on mine that will work for the actual problem.</p> <hr> <p>Here's what I've come up with:</p> <p>Create an instance of a dictionary (which is implemented as hash table, giving O(1) in normal situations). Keys are integers, values are hash sets of integers (also O(1)) – <code>var D = new Dictionary&lt;int, HashSet&lt;int&gt;&gt;</code>.</p> <p>Iterate through the array <code>A</code> and for each integer <code>n</code> with index <code>i</code> do:</p> <ol> <li>Check whether keys <code>n-1</code> and <code>n+1</code> are contained in <code>D</code>. <ul> <li>if neither key exists, do <code>D.Add(n, new HashSet&lt;int&gt;)</code></li> <li>if only one of the keys exists, e.g. <code>n-1</code>, do <code>D.Add(n, D[n-1])</code></li> <li>if both keys exist, do <code>D[n-1].UnionWith(D[n+1]); D[n+1] = D[n] = D[n-1];</code></li> </ul></li> <li><code>D[n].Add(n)</code></li> </ol> <p>Now go through each key in <code>D</code> and find the hash set with the greatest length (finding length is O(1)). The greatest length will be the answer.</p> <p>To my understanding, the <em>worst case</em> complexity will be O(n*log(n)), only because of the <code>UnionWith</code> operation. I don't know how to calculate the average complexity, but it should be close to O(n). Please correct me if I am wrong.</p> <p><strong>UPD:</strong> To speak code, here's a test implementation in C# that gives the correct result in both of the OP's examples:</p> <pre><code>var A = new int[] {4, 5, 1, 5, 7, 6, 8, 4, 1}; var D = new Dictionary&lt;int, HashSet&lt;int&gt;&gt;(); foreach(int n in A) { if(D.ContainsKey(n-1) &amp;&amp; D.ContainsKey(n+1)) { D[n-1].UnionWith(D[n+1]); D[n+1] = D[n] = D[n-1]; } else if(D.ContainsKey(n-1)) { D[n] = D[n-1]; } else if(D.ContainsKey(n+1)) { D[n] = D[n+1]; } else if(!D.ContainsKey(n)) { D.Add(n, new HashSet&lt;int&gt;()); } D[n].Add(n); } int result = int.MinValue; foreach(HashSet&lt;int&gt; H in D.Values) { if(H.Count &gt; result) { result = H.Count; } } Console.WriteLine(result); </code></pre>
    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