title: Optimal way for finding index of 'greatest value less than' in Numpy array
body: <p>I have a sorted numpy array <code>X</code> and also two constants <code>k</code> and <code>delta</code> that are not in <code>X</code>. I would like to find the index corresponding to, the largest value in <code>X</code> less than or equal to <code>k</code> and the value must be within <code>delta</code> of <code>k</code> i.e. I want</p>
  
  <pre><code>max {i | k - delta &lt;= X[i] &lt;= k }    (1)
  </code></pre>
  
  <p>Note this set may be empty in which case I will return <code>None</code>. The way I'm doing it I currently feel is unoptimal as it doesn't take advantage of the fact <code>X</code> is ordered at the first step</p>
  
  <pre><code># Get the max from the set of indices in X satisfying (1)
  idx = np.where((k-delta &lt;= X) * (X &lt;= k))[0].max()
  </code></pre>
  
  <p>I'm not sure how clever Numpy can be when doing this as it doesn't already know <code>X</code> is sorted hence the <code>(k-delta &lt;= X) * (X &lt;= k))</code> will I assume take longer than necessary. Note we can use the <code>.max()</code> as we know ourselves the array is sorted.</p>
  
  <p>What would be a more optimal way of doing this?</p>