title: Find all subarrays of fixed length with a given ranking
body: <p>I have an array of numbers, for example:</p>
  
  <pre><code>A = [1, 5, 2, 4, 3]
  </code></pre>
  
  <p>and an array that determines a rank, for example:</p>
  
  <pre><code>B = [0, 2, 1]
  </code></pre>
  
  <p>My goal is to find all the subarrays of A that "obey" the rank B. If a subarray obeys the rank, that means that the i-th smallest element of the subarray must have <code>B[i]</code> as its (subarray) index. So for a subarray to match, the smallest element within it must be in position 0, the second smallest must be in position 2, and the biggest element must be in position 1.</p>
  
  <p>So for example here, there are two subarrays of A that match the ranking: [1, 5, 2] (because A[0] &lt; A[2] &lt; A[1]) and [2, 4, 3].</p>
  
  <p>So far, I've managed to find a solution that has an <code>O(mn)</code> (m is len(A) and n is len(B)) time complexity, it iterates over all the subarrays of length 3 and verifies if they are correctly ordered:</p>
  
  <pre><code>A = [1, 5, 2, 4, 3]
  B = [0, 2, 1]
  m = len(A)
  n = len(B)
  for i in range(m - n + 1):
      current_subarray = A[i:i + n]
      # we now do n - 1 comparaisons to check whether the subarray is correctly ordered.
      for B_index in range(n - 1):
          if current_subarray[B[B_index]] &gt; current_subarray[B[B_index + 1]]:
              break
      else:
          print("Subarray found:", current_subarray)
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Subarray found: [1, 5, 2]
  Subarray found: [2, 4, 3]
  </code></pre>
  
  <p>This works, but I was wondering if there was a <strong>better optimized algorithm</strong> (better than <code>O(mn)</code>) to fulfill this task.</p>