How to predict the length and offset of the sequence in and array? I have an input string similar to ABCDEFGHIJK from which a series of sequences has been generated into an array as following:

00: 'ABCD'
01: 'BCDE'
02: 'CDEF'
03: 'DEFG'
04: 'EFGH'
05: 'FGHI'
06: 'GHIJ'
07: 'HIJK'
08: 'ABC'
09: 'BCD'
10: 'CDE'
11: 'DEF'
12: 'EFG'
13: 'FGH'
14: 'GHI'
15: 'HIJ'
16: 'IJK'
17: 'AB'
18: 'BC'
19: 'CD'
20: 'DE'
21: 'EF'
22: 'FG'
23: 'GH'
24: 'HI'
25: 'IJ'
26: 'JK'
27: 'A'
28: 'B'
29: 'C'
30: 'D'
31: 'E'
32: 'F'
33: 'G'
34: 'H'
35: 'I'
36: 'J'
37: 'K'


The sequences in the array above has been generated by following these steps:


Set length to 4
Set offset to 0
Pick a sequence of length long characters from "ABCDEFGHIJK" (Input string)
Put the sequence at the bottom of output array
Increase offset by 1
If offset + length &lt;= 11 (Input string's length) then goto 3
Decrease length by 1
If length &gt;= 1 then goto 2
Exit


By looking at the output array, sequences in the array have a length which starts at 4 and gradually goes down to 1, and also an offset which is where in the input string the sequence has begun.

What I'm trying to achieve is to "predict" both the length and the offset of a sequence at a given index in the output array, only having the input string's length and index in the output array.

I'm preferably looking for a formula, however an algorithm which doesn't involve simulating the output array will work as well.
Let $n$ be the length of the string, and $k$ be the maximum length of the substring. Thus the first $n-k+1$ positions correspond to length $k$, the next $n-k+2$ to length $k-1$, and so on. Denote by $i_\ell$ the index at which substrings of length $k-\ell$ begin. We have
$$
i_\ell = \sum_{t=k-\ell+1}^k (n-t+1) = \frac{[(n-k+1)+(n-k+\ell)]\ell}{2} = \frac{\ell^2}{2} + \ell\left(n-k+\frac{1}{2}\right).
$$
Given $i_\ell$, we can solve for $\ell$:
$$
\ell = \sqrt{(n-k+1/2)^2+2i_\ell}-(n-k+1/2).
$$
Monotonicity shows that for arbitrary $i$, the floor of this expression still gives the correct value of $\ell$. This suggests the following algorithm:


Let $i$ be the input index.
Calculate $\ell = \lfloor \sqrt{(n-k+1/2)^2+2i}-(n-k+1/2) \rfloor$.
Calculate $i_\ell = \ell^2/2 + \ell(n-k+1/2)$.
Output length=$k-\ell$ and offset=$i-i_\ell$.


For example, let's take $i = 10$ in your case (where $n = 11$ and $k = 4$). Then $\ell = \lfloor \sqrt{7.5^2+20}-7.5 \rfloor = \lfloor 1.23\ldots \rfloor = 1$ and $i_\ell = 1^2/2 + 1\cdot 7.5 = 8$. Hence the length is $4-1=3$ and the offset is $10-8=2$.