title: Find array indices of values greater than given distance in O(n)
body: <p>Given an array with indices 1 ... n and corresponding values x<sub>1</sub> ... x<sub>n</sub> and the values are sorted in increasing order, left <em>f(i)</em> be the first index (smaller than i) of the greatest value (from the right to the left) with |x<sub>i</sub> - x<sub>f(i)</sub>| > 5. For example given (x<sub>1</sub>, x<sub>2</sub>, x<sub>3</sub>, x<sub>4</sub>) = (6, 7, 12, 14) then f(4) = 2 because the index of the greatest value (from the right to the left) with a distance greater than 5 is 7 (as 14-12 would only be a distance of 2), f(3) = 1 and f(2),f(1) are undefiened (no cycles/modulo because index f(i) must be smaller than i). <br><br>
  I'm searching for an algorithm that finds f(n),f(n-1), ..., f(1) alltogether in time complexity O(n). 
  The naive version has O(n<sup>2</sup>) only.</p>