title: Minimum Excluded Value of subarray
body: <p><code>mex</code> here refers to the <a href="https://en.wikipedia.org/wiki/Mex_(mathematics)" rel="nofollow noreferrer">minimum excluded value of an array</a>.</p>
  
  <p>I have and array with about 300000 elements. Then I have 50000 pairs of a and b (1&lt;=a&lt;300000 b&lt;=300000) and computer need to tell me the MEX of array in range from a to b.</p>
  
  <p>Example: </p>
  
  <pre><code>int a[10]={0,1,2,3,0,4,5,4,8,6};
  a=3;
  b=8;
  </code></pre>
  
  <p>We start from a[2] end and at a[7] , and MEX will be 1</p>
  
  <p>I made a funtion, but it takes 30 seconds for one pair which is not what I need.</p>
  
  <pre><code>int Mex=0;
  vector&lt;int&gt; a;
  // fill vector
  while (find(a.begin(),a.end(), Mex )!= a.end())
  {
      Mex++;
  }
  cout &lt;&lt; Mex;
  </code></pre>
  
  <p>So the quextion is: how to make fast MEX function with C++, so that computer can do 50000 various MEX operations for 300000 elements in 5 seconds? Memory limit is 900MB. </p>