title: Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?
body: <p>I have five long integers p, q, s, m and x. An array numbers[] is created by the following formula.</p>
  
  <pre><code>numbers[0] = s;
  for(int i=1; i&lt;numbers.Length;i++){
      numbers[i] = (p * numbers[i-1] + q) % m;
  }
  </code></pre>
  
  <p>The first value of numbers (numbers[0]) is s. </p>
  
  <p>What is the most efficient way to find index j where <code>i &lt; j</code> and <code>|numbers[j] - numbers[i]| &lt;= x</code> or <code>|numbers[j] - numbers[i]| &gt;= m-x</code>.</p>
  
  <p>For instance, in a case where p = 3, q= 7, s= 1, m= 29 en x= 1 the array will be:</p>
  
  <p>numbers[0] = 1, numbers[1] = 10, numbers[2] = 8 and numbers[3] = 2.</p>
  
  <p>In this case index j would be 3, because <code>numbers[3] - numbers[0]&lt;=x</code>, because x is 1. </p>
  
  <p>I thought about using something such as a variant of counting sort or radix sort but I can't get anything to work.</p>