[Question]
  [
We all know the age-old debate - should strings be length-prefixed or should they be null-terminated? Well, you've run into a blob of binary data and apparently whoever made it decided that the best solution would be a compromise and length-terminated their strings.
For the purposes of this challenge, a length-terminated sequence is a series of one or multiple integers such that the last item in the sequence is equal to the number of items in the sequence, and no prefix of the sequence is also a valid length-terminated sequence. For example, `[1]`, `[15, 2]`, and `[82, 19, 42, 111, 11, 6]` are all valid length-terminated sequences. `[15]` is *not* a valid length-terminated sequence (it hasn't been terminated), and neither is `[15, 2, 3]` (it should have ended at the `2`).
Your task is to write a program or function that takes a list of integers, and outputs all the valid length-terminated sequences within that list. For example, for the input list `[7, 6, 5, 4, 3, 2, 1]`, valid length-terminated sequences would be `[1]`, `[3, 2]`, `[5, 4, 3]` and `[7, 6, 5, 4]`.
* You must output *all* valid length-terminated sequences found in the input, in any order. The order may vary between runs of your program. You must output *no* length-terminated sequences not found within the input, and *no* sequences found within the input that aren't valid length-terminated sequences.
* If some length-terminated sequence occurs multiple times within the input, it should also be output multiple times. The sequence `[9, 8, 3]` occurs twice in the input `[9, 8, 3, 9, 8, 3]` (once starting with the first number, and again starting at the fourth number) and should thus be output twice.
* A number may be part of more than one length-terminated sequence. For example, the input `[9, 9, 2, 4]` contains both the length-terminated sequences `[9, 9, 2, 4]` and `[9, 2]` and they should both be output.
* You may assume all numbers are between 0 and 127 (both inclusive).
* You may assume the input contains no more than 126 numbers.
* You may *not* make any further assumptions about the input, including the assumption that the input contains at least one valid length-terminated sequence.
* You may take input or produce output by any of the [default methods](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods). Input and output formats are flexible (within reason) and the input format need not be the same as the output format. However, the your input and output formats must not depend on the input, and must not vary between runs of your program.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") - make your code as small as possible.
# Examples
```
[input] -> [output], [output], ...
[7, 6, 5, 4, 3, 2, 1] -> [7, 6, 5, 4], [5, 4, 3], [3, 2], [1]
[90, 80, 70] -> (no output)
[1, 2, 3, 4, 5] -> [1]
[100, 0, 2, 2, 2, 0, 0, 2, 4] -> [0, 2], [2, 2], [2, 2], [0, 0, 2, 4], [0, 2]
[] -> (no output)
[54, 68, 3, 54, 68, 3] -> [54, 68, 3], [54, 68, 3]
[4, 64, 115, 26, 20, 85, 118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37, 113, 64, 81, 99, 30, 83, 70] -> [85, 118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14], [118, 9, 109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37], [109, 84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13, 33, 55, 119, 54, 19], [84, 64, 48, 75, 123, 99, 32, 35, 98, 14, 56, 30, 13], [14, 56, 30, 13, 33, 55, 119, 54, 19, 23, 112, 58, 79, 79, 45, 118, 45, 51, 91, 116, 7, 63, 98, 52, 37, 113, 64, 81, 99, 30], [45, 118, 45, 51, 91, 116, 7]
```
  
[Answer]
# [Python 2](https://docs.python.org/2/), 67 bytes
```
f=lambda l:l and[l[:x]for i,x in enumerate(l)if-~i==x][:1]+f(l[1:])
```
[Try it online!](https://tio.run/##LVDLboQwDDx3vyJHUKdSnAckSHxJlAPVgoqUZVcsleilv05ttsrD9tgeJ/P42b7uizmOqS/D7fM6qNIVNSzXVFK35@m@qhm7mhc1Lt@3cR22sSr1PH38zn2/59RRfp@qkqjL9bGNz@2pepVSiwYeDhYGlKFS1AgarRafGLSc9GegNTQDsl6eE1iOd2gCV/5bgdhzIPIwDQxTeg4CIkhHhDPpAlpGjUWMsDzIIwYQT2tgNcjCMqP0RSHmm0uJDDw3RtnuRcrGEyJx0ID/Y4XHM2HLiJVRgc4Z/AwrP8uXi8glIohepxjd5e2xzsumpkri@vgD "Python 2 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Ẇ=J$Ḅ⁼ɗƇ1
```
A monadic Link accepting a list of integers which yields a list of lists of integers.
**[Try it online!](https://tio.run/##y0rNyan8///hrjZbL5WHO1oeNe45Of1Yu@H///@jDQ0MdBSAyAiG4FyTWAA "Jelly – Try It Online")**
Or `ẆĖE€Ḅ⁼ʋƇ1` - [Try it](https://tio.run/##y0rNyan8///hrrYj01wfNa15uKPlUeOeU93H2g3///8fbWhgoKMAREYwBOeaxAIA "Jelly – Try It Online").
### How?
```
Ẇ=J$Ḅ⁼ɗƇ1 - Link: list of integers
Ẇ         - all sublists
        1 - set right argument to 1
       Ƈ  - filter keep those (sublists) for which:
      ɗ   -   last three links as a dyad - i.e. f(sublist, 1):
   $      -     last two links as a monad i.e. g(sublist)
  J       -       range of length = [1,2,3,...,length]
 =        -       (sublist) equals (that)? (vectorises)
    Ḅ     -     from binary
     ⁼    -     (that) equals (1)?
```
[Answer]
# [JavaScript (V8)](https://v8.dev/), 65 bytes
Prints the results.
```
a=>a.map((_,i)=>a.every((v,j)=>j<i|v-++j+i||print(a.slice(i,j))))
```
[Try it online!](https://tio.run/##LVDLboMwELz3Kzj04CgTZGMMdlNy66HfQKPGQolq1FJEUqQq5NvpONSv3ZndnbXd@tGfmyH0l81o51M1@2rn0y/fC/GOsIrgOB6HXyFGtITtc5jGzXrdrsM09UPoLsKn58/QHEVgAse8resSBQxyaGRQeyS1k7ASpYy@IqkZNHcgJSSJOBcvj3TcJkdhmflvI0Uvh1IGWYGMkobAwkFJB3sP5hYl2UzDOWg2MnAWit0KaAmloakY61wU5slUpTIYFrq48kWUxig4RVCA79FRx1CwJKNjK6vuPXgNHV@2f0hP38OLbz6ET6pdck2W7zm8dv3P5SmpH6/@tn/rDqttchKe5xKnc1vNfw "JavaScript (V8) – Try It Online")
### Commented
```
a => a.map((_, i) => // for each value at position i in a[]:
  a.every((v, j) =>  //   for each value v at position j in a[]:
    j < i |          //     yield true and go on with the next iteration if j < i
    v - ++j + i      //     or v is not equal to j - i + 1 (increment j)
    || print(        //     otherwise, print ...
      a.slice(i, j)  //       ... the sublist of a[] from i to j - 1
    )                //     and break (print() returns undefined)
  )                  //   end of every()
)                    // end of map()
```
[Answer]
# [Python](https://docs.python.org/2/), 62 bytes
```
f=lambda l:l and[l[:x]for x in l if[x]==l[x-1:x]][:1]+f(l[1:])
```
[Try it online!](https://tio.run/##LVBJbsMwDDzXr9AxQaeAqMWWBPglgg4uUqEGVCdIfHBf75BOoIXkkBxKc/tff6@L2fc6tunv@zKplpqalktuOW2lXu9qU/Oimppr3so4trx9EWdKTlQ@66llSuW8rz@P9aFGlfOAHh4OFgZUoHLUCBqDFp8YtJz0R6A1NAOyXp4TWI536ANXvq1A7DkQeZgehik9BwERpCPCkXQBA6PGIkZYHuQRA4in9bAaZGGZUfqiEPPNpUQGnhujbPciZeMJkTjowf@xwuOZcGDEyqhAxwx@hpWfla4TpUQEEesQI3Uft/u8rKqeJD7vTw "Python 2 – Try It Online")
This is a subtle improvement on [@xnor's solution](https://codegolf.stackexchange.com/a/194242/20080).
The solution we wish we could use is
```
f=lambda l:l and[l[:x]for x in l if x==l[x-1]][:1]+f(l[1:])
```
However, this will crash on an out-of-bounds index. xnor avoided this problem by using the index at which the value `x` appeared rather than actually indexing into `l`, but this required using the expensive `enumerate` function.
Instead, I used `[x-1:x]` to slice out the length-1 slice including the index we're interested in. Slicing in Python is much more permissive with respect to out of bounds errors. Now, we only need to check that the part of the list sliced out is `[x]`.
Works in Python 2 or 3.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 12 bytes
```
a₁ᶠ{a₀.l~t}ˢ
```
[Try it online!](https://tio.run/##PVBBTsMwEPzKqmcLee24ibnwkKiH0AMgVa0ElRCqWrU5VHDjCaiXXrlQIb7AK5KPhBm7VMpm1zO7M2vfPjbT@5fZ4s4Nq@eRTBfzZfMwf7oedT9vN/3@s29bVN3Xe/967L5PqNc4DE3f7rrTxwp5ezXbLNe/h2GopS6NjI0EI4URb8QZ0YmROlojFaK0PGkifGoKCbDgbELzdzkWpBkBveMqTV1K4qwRqvB0sHZ0CgTQEZEtftW5qwBWknSQiSA89wAQQSi3gYKHgoL3tEpKMXsyc1AVU4FSMUfx78ci4HZRCUCLz@GzfKBVSdznZSo978CNfXobmfwB "Brachylog – Try It Online")
```
a₁ᶠ{      }ˢ    For every suffix of the input, if possible,
    a₀.         find the shortest prefix
       l        the length of which is
      . ~t      its last element.
```
An alternate [generator](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/10753#10753) solution using `s`, taking inspiration from Jonathan Allan's Jelly solution:
# [Brachylog](https://github.com/JCumin/Brachylog), ~~13~~ 12 bytes
```
s.i₁ᶠ=ˢ~gh~l
```
[Try it online!](https://tio.run/##PVBBagJBEPxK43kI0zM77k4g5CGLB/WggijEQAghgnuQ5JYniBevuURCvpBX7H5kUzVjhO2d7qruqp6ZPIyn8@fleub6l6eBTNerx/FitbkdtD/v993@s2ua9nxA0X59dG@n9vuM/BVFv7lZdM0O5N3vcTubb5d9X0tdGhkaCUYKI96IM6IjI3W0RipEaVlpInxqCgmw4GxC83ctC9KMgN5hlaauKXHmCFV4Olg7OgUC6Ig4LX7VpasAVpJ0kIkgPPcAEEEot4GCh4KC97RKSjF78uSgKqYCpWKO4t@PScDtohKAFp/DZ/lAq5K4z8tUetmBG/v0NjL6Aw "Brachylog – Try It Online")
```
 .              The output is
s               any substring of the input
  i₁ᶠ           such that a list of all elements paired with their 1-indices
     =ˢ         filtered by equality
       ~g       has only one element, of which
         h      the first element
          ~l    is the length of the output.
```
[Answer]
# [Prolog (SWI)](http://www.swi-prolog.org), 67 bytes
```
X-Y:-X+Y+1.
[_|X]-Y:-X-Y.
[H|T]+[H|X]+N:-X=[],N=H,!;M is N+1,T+X+M.
```
[Try it online!](https://tio.run/##KyjKz8lP1y0uz/z/P0I30ko3QjtS21CPKzq@JiIWzNeNBPI8akJitYFkRKy2H1DMNjpWx8/WQ0fR2lchs1jBT9tQJ0Q7QttX7///aEMdIx1jHRMd01jdCD0uaxgEAA "Prolog (SWI) – Try It Online")
Uses the `-/2` predicate.
There are two parts to the code.
```
[H|T]+[H|X]+N:-X=[],N=H,!;M is N+1,T+X+M.
```
Essentially computes the shortest prefix that end in its length. However it only does this when called with `X+Y+1`.
Then we have the main predicate `-/2`
```
X-Y:-X+Y+1.
[_|X]-Y:-X-Y.
```
This matches all the prefixes of `X` than end in their length:
```
X-Y:-X+Y+1.
```
and anything that matches the tail of `X`.
```
[_|X]-Y:-X-Y.
```
That turns our predicate over prefixes to a predicate over sublists.
[Answer]
## Haskell, ~~69~~ 64 bytes
```
((1#)=<<).scanr(:)[]
l#(a:b)|l==a=[[a]]|m<-l+1=(a:)<$>m#b
_#_=[]
```
Edit: -5 bytes thanks to @Sriotchilism O'Zaic
[Try it online!](https://tio.run/##PVDLboMwELzzFSvRA1adiMU44Aj30nOlHCu5KHKi0kY1KAo95ttLd20UifVjZnZm8beffz5DWAb7sRQF5sJ2ndjOZz/dir1wfRbywu9P4h6s9dY53/f3sduEZ7SEi@7pZcxP2TE/Wtcvo79MYGH01zcorrfL9AtbGAS4DMA1EnYStIRagpJQScBeMmFKCS1VU6Y7RlJFoV6hkvgy4ul7XOskSKumjl0bex/HxPCNCpHyKxqj4kzNAGkM7SUt7aqqCWuYrMjIEKF4Hs1GYIhDHoxMFJkgSRTnRTOTgnnnXkRq1OxmUtVrJDvxWdO/GmSM7PiBOJD0mgMbxlUaqcV1Ep5bxbfqs@XvPAT/NS@b99fD4R8 "Haskell – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 59 bytes
```
(>>= \l->take 1[take x l|(i,x)<-zip[1..]l,i==x]).scanr(:)[]
```
[Try it online!](https://tio.run/##LU/LboMwEPwVH3oI0oC8GIMdFf6gX@BwsFBoURyEQg6o6reX7pLKj92Z3Z2xv@J6u6a0j@1lP3Vdqy4p757xdlUUjrCp9HOasGXv@fe0BCqKPmFq263PinWI8@N0zkK/3@M0q1bd4/Khlsc0P9WbADWqEBrUsKhgUIJ6qOA1nEajJScmDRftAbSGZkLWK6uElmMr1I47/6NQnFUgsihrlCxpGTh4kPZwR7FyaJgtDbyHYSML70DsVsNokIFhRZnzIsw3txKVsDzoZVcvUQ6W4IlBDf6PER3Lgg0zRqwcHR78DCM/6/ffYUzxc93zYVn@AA "Haskell – Try It Online")
**60 bytes**
```
f l@(h:t)=take 1[take x l|(i,x)<-zip[1..]l,i==x]++f t
f _=[]
```
[Try it online!](https://tio.run/##LU/LasMwELz3K/bQQ0ImQWs9LJUa@gP9AmGKCXFjogTT@GBKv73urlP02J3Z3Rnp3N0vp1KWpafytjm/TNtm6i4n4ryGmcrPZsC8fd1/D2Pmw6EtGJpmbne7nqannj6a3C7XbrhRQ9dufKfxa7hN9KyAesq5RoCHg0UFbkE5GUSD2mjOQlop@hUYAyOErkfmlNbjHUKUzv@olGQOzB5VQCWSXkBEApuEuBZdRC1sZZESrBh5pAgWtwBrwBZWFHUuqbDc0spcwctg0u0eohI8I7GAAPmPVR0vgrUwVq0irx7yDKs/a5ffY1@6z/uyP47jHw "Haskell – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/), 59 bytes
```
/(?<!\d).*?\b(\d++)(??{$1^1+$&=~y, ,,||"(*PRUNE)".say$&})^/
```
[Try it online!](https://tio.run/##NU/LasMwELz7K6bGBDt2HK0l2VIf@NRjSyn0ZhIS0kMgOKHpJTTtp1cdJRQJ7c7s7oz28P6xs@G4OiHdjrfIluldmOf9/c2wKeppP6zzYVOWRd73X5kspMwmDz@nClV1Pqf59OX17fmxSGvOZ5PvYjEPoUMLCwONBoLEKziFTiEREpoFy1QpKMJ4rplBgsQatI49/zFhNBCxaFo0FLIEDh6iPNylaBw6so2G99A0sPAOQpcWWkE0NPXinI@yfNkq0sBy0MdrrqIMVuCFoAV30FHHUrAjo6OVk4sHv6HjPr/7w@d2Px7DbLUbw@zJ1kr@AA "Perl 5 – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 38 bytes
```
ＩＥΦＬθ∧‹§θι⁺²ι⬤✂θ⁻⊕ι§θιι¹⁻λ⊕μ✂θ⁻⊕ι§θι⊕ι
```
[Try it online!](https://tio.run/##lU/BasMwDP0VHxXwIIrjJmGnMhgUVijsWHoIiWkNrrcm3tjfe09LC@uxAtnWe09P8nDqp@GjDznvJh8TvfRzom3/Sa8@JDfRm4vHdKJLodU6jijnmdZpE0f3QxetPPBd@JqpkreIQqD34Acn7NZHUJs4TO7sYnIjif6uHbXXioubOGj1X38uJLR60PKel3jOeb@vtVohma1W1QpZatVaAVqtOtwljvaqqoE1QlYGJAiDPxoAHQiGwMLBwIHBG6T9c4LQygzc0siMLitW3ZL1bZ48LMOOBYBXg7lmsbcyqhHcLMu0fN1BNgbWlIdDfvoOvw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
    θ                                   Input array
   Ｌ                                    Length
  Φ                                     Filter over implicit range
       §θι                              Value at current index
      ‹                                 Is less than
          ⁺²ι                           Current index plus 2
     ∧                                  And
             ⬤                          For all elements of
              ✂θ                        Slice of input array
                ⁻⊕ι§θι                  From potential sequence start
                      ι¹                To current index
                         λ              Value
                        ⁻               Does not equal
                          ⊕μ            1-indexed index
 Ｅ                                      Map over matching indices
                            ✂θ⁻⊕ι§θι⊕ι  Extract sequence including length
Ｉ                                       Cast to string for implicit print
```
[Answer]
# [J](http://jsoftware.com/), ~~28~~ 22 bytes
```
a:-.~((#={:)\{.@#<\)\.
```
[Try it online!](https://tio.run/##xVDLSsRAELzvVxTuYRJIwjyTeeyCIHjy5DkXEYN48QMW/PVYM3mIB0ERkTy6p7qruqZf5qtOTDhHCDSQiPzaDjf3d7fzQ2y7t6o6ni@xHi/d9fE01mM314fD0@PzK6oJA3o4WBhoqBpt3JC0wClXEqomsrqwhMhtE4KElxjkrqWoYUhyRaeJJ5I@ilLSmC7PktnSVtKEgjOuYe3IhZx8HizEruoses@hayyKG0bylu79PFso5aB7aPp3PHgEugvwpWg9BqLaIAQYXsgheCjeqoeRUAaG4zIvZHn@2aqUhiMx5NcuogxOISge@rxUk3UcBQciJo/yqsygDcM11sjmf@CIu/kH8@k349J3ielvN56@oMzv "J – Try It Online")
Take the empty boxes `a:` away from `-.~` the following result:
For each postfix of the input `\.` do the following:
Filter `#` the boxed prefixes of the current input `<\` using the boolean mask which asks, also for each prefix of the current input, "Is the length equal to the last item?" `(#={:)\`.
From those boxed prefixes that *do* pass the filter, take the first one `{.@`. If *no* prefixes pass the filter, this will return the empty box `a:` (ie, the zero value of a list of boxes).
Note for J-ers: The function always returns a list of boxes, each of which contains a list of numbers. This (desirable) consistency is what creates some of the seeming inconsistencies required to correctly specify the expected values of the test cases: 
* `(,:1)` -- a list containing 1
* `,:<,:1` -- a boxed list containing the list containing 1 (remember: a single box is an atom)
[Answer]
# [C (clang)](http://clang.llvm.org/), ~~101~~ ~~98~~ ~~97~~ 95 bytes
```
j;f(*l,z){for(l+=z;z;)if((j=-*--l)&&j>~z--&&j-~l[-1]|!z)for(puts("");printf("%d ",l[++j])*j;);}
```
[Try it online!](https://tio.run/##TVLbTuMwEH3PV3iDQHYzYeM4V0z5kWweUC/gKKSoFwmllF8vM3bXchTXM3POnDlxvUpX4@v0dr0OessXI8zivN3t@ZgsZz1rYbacD8t0kaajeHgYXn7mNMU9/Rm7VPbff2ZB7M/T8cDjWOjPvZmOWx7fr1kMY5ckQy8Wgxb6cr0z02o8rTfs@XBcm93j@0uEXDadPg6y65dnCQUoXBVUF@2hnKAaWAWsBFYAU8ByYDKgKKK0GbAGV50FSOF0qUHZ5jIASwtm2JNZhnt9WgTUiqhBXlNeol7VWGUfBpyGOFTHJSV6z/ETcnJZUgHZLe4Z/jQ3VoG1msAcJVsEFPnGQouAJPeooFBBIq5orFVq3XzaqVFK7CpJqnWr@D@PghJPo5VUQC06VuXkSxpVU105M428eSDH6nau0RtffMHBzBtxth9qlpmO3H/OeNzhBYjoOlDZPBMvlTpJDLI96X4NMXx1pkfuJcLrZUQIeszXevZkdfltcgD9m2KrElkzH69m4oKdyaa9VVAh6uIcah8rUD4uoPBxCa2PK8h8XAc6jfWww@E2Y@KvyzkaEMiKIobPfnM87SeGZ3O5/gI "C (clang) – Try It Online")
-1 thanks to @ceilingcat 
```
j; 
f(*l,z){ // function taking a pointer and size  
for(l+=z;z;) // move pointer to the end for backward iteration 
if( // cascaded conditions : 
  (j=-*--l) // skip if value is 0 while assigning to j its negation 
  &&j>~z-- // skip if j>z ( before array begins) 
  &&j-~l[-1] // skip if previous value is 1 less than value  
  |!z // if z =0 previous value doesn't care 
) 
for(puts("") // newline to separate lists obtained 
   ;printf("%d ",l[++j]) // print each value from j before l position 
    *j;);} // to l (j=0) 
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 40 bytes
```
{~<<m:ov/(<<\d+>>)+?%\s<!{$0-$0.tail}>/}
```
[Try it online!](https://tio.run/##PU/tasMwDPyfp7jRbEmWrrXij9jDS18kUApboKVdRjMGpXSvnklJGf6QdDrd2V8f56MbTxc8dXgbr78xnl77n3UeY/teNk1Rbh7bIT5cU/WSqtX3bn@8NevbuPhnxpgzsx2K500@j9zZVJJQk2TYXdDl6bZYHfr9Z54tkRXo@nOS1XCwMNCoQNkyyYKCV6iV5MSg5qadCqWgGJA1Z0ZgOdbAeWbeo0CcGRBZVA4VS1ouPAJIBfipaTxqRiuNEKDZyCJ4ELs5aAXS0Kwoc0GE@WYqUQXLg0G2mUU5WEIgLhz4P1p0LAvWjGix8jR58DO0/Gz8Aw "Perl 6 – Try It Online")
Regex-based approach working with strings of space-separated numbers.
**35 bytes** when working directly with ASCII strings:
```
{~<<m:ov/(.)+?<!{$0-$0.tail.ord}>/}
```
[Try it online!](https://tio.run/##PU/tbsIwDPzfp7gJtLYrC3Hz0WTqyovwB8GqgWCd2mkTQuzVO7ugKR/2nc/n5POtP/rxdMZji9fx8lvXp5fue5mpvFjVD5e5fp5r9bXZH1XX767N8jrO/kV1na13xXrIn1aSNE1@76CCRJokw@aMNlM/3Duo7Xs/5E2jJnDo9h9ZukCao@36JK3g4WBhUILSRZJGjaBRacmJScNFNwGtoZmQdcus0HKchQ@svEehOLMgcig9SrZ0DAIiSEeEqWgDKmZLgxhheJBDDCCe5mE0yMCwo/RFMeabpUQlHDdG2fZmysERIjHw4P8Y8XFsWDFjZFSgaQY/w8jPxj8 "Perl 6 – Try It Online")
### Explanation
```
{                                      }  # Anonymous block
    m:ov/                             /   # Find all overlapping matches
          <<       # Left word boundary
            \d+    # One or more digits
               >>  # Right word boundary
         (       )+?     # One or more numbers, lazy
                    %\s  # Separated by space
                       <!{          }>  # Negative boolean condition check
                          $0-$0.tail    # Count of numbers minus last number
 ~<<  # Stringify matches
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~72~~ 70 bytes
-2 bytes by somehow typing `a.length` instead of `a.size` when constructing the range, but still using the correct function later down the line when checking the sequences.
```
->a{(r=0..a.size).map{|i|r.map{|j|a[i,j]}.find{|s|s[-1]==s.size}}-[p]}
```
[Try it online!](https://tio.run/##PVDLboQwDLzzFT700EoBxYQs5EB/JEIV1ULLaneFoA@1wLfTcbJdKSb2jD1jMn2@/ux9vafP7fI41TrL2mwefrun7NKOyzqsU0xOa@sHdWq2rB@ux2Wd19mn3NT1HNq3LfVjs@3f78O5o7fuY06IRup999We6eGlSbrrcfelooMiq6hQZBTlirhJvNOKKkSpUXCATWixUmswOoDx3MsCLI5F46EKI/cUsKQIZrjlMM3FxAqABodb41PdugpgpZA5VBwIIzsAcCBYNoGCgQKDN@IUlFy0lFsGmTFlRcrFKP79JLH4M8cCQEsewkR5K1al4CYuU/FtB9nYhGf5Aw "Ruby – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-mR`](https://codegolf.meta.stackexchange.com/a/14339/), 12 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
WsV
¯ÒUbÈÉ¶Y
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LW1S&code=V3NWCq/SVWLIybZZ&input=WzQsIDY0LCAxMTUsIDI2LCAyMCwgODUsIDExOCwgOSwgMTA5LCA4NCwgNjQsIDQ4LCA3NSwgMTIzLCA5OSwgMzIsIDM1LCA5OCwgMTQsIDU2LCAzMCwgMTMsIDMzLCA1NSwgMTE5LCA1NCwgMTksIDIzLCAxMTIsIDU4LCA3OSwgNzksIDQ1LCAxMTgsIDQ1LCA1MSwgOTEsIDExNiwgNywgNjMsIDk4LCA1MiwgMzcsIDExMywgNjQsIDgxLCA5OSwgMzAsIDgzLCA3MF0)
```
-mR         //Map each element in the input to the following program, and join with newlines
            //U is the element being mapped over
            //V is the index
            //And W is the original array
WsV         //Set U to the original array, removing the first V(index) elements
¯           //Take the first n elements, where n is the following:
   Ub       //  The first index in U which satisfies the following condition
     ÈÉ¶Y   //    Where the element at that index minus one equals the index
  Ò         //   Add 1 to that result
```
[Answer]
# Curry, 33 bytes
Tested in both [KiCS2](https://www-ps.informatik.uni-kiel.de/kics2/) and [PAKCS](https://www.informatik.uni-kiel.de/%7Epakcs/).
```
f(_++u++x:_)|x==1+length u=u++[x]
```
Curry's powerful patterns let it beat [the Haskell answer](https://codegolf.stackexchange.com/a/194326/56656) once again!
To test it you can use [Smap](https://smap.informatik.uni-kiel.de/smap.cgi?new/curry). Just select `KiCS2 2.2.0` or `PAKCS 2.2.0` with the `/all-values` option and paste the following complete code:
```
f(_++u++x:_)|x==1+length u=u++[x]
main=f [7, 6, 5, 4, 3, 2, 1]
```
[Answer]
# [PHP](https://php.net/), 90 bytes
```
for(;++$i<$c=count($argv);)for($j=$i;$c>$j;$j-$i-$n?:$c=print_r($$i))$$i[]=$n=$argv[$j++];
```
[Try it online!](https://tio.run/##ZVHRToMwFH3nK25IH0A6pRTGsEOffTLxFYkhBDfI7AgDE2P89npvgfkgo/T23NNzTrv@2Jv9Y3/sHYeNzWW8QA6FA/gUKYcth4RDzEFyiDiIks@tLOSww5GGKyIsQVpycgVD5IS2M7/XZbxS1jnBfdudVbiWa4/WOITALBFGisg9IQBZGc4hfnYLK0YspWaEUhk2JOVCIMOGoHSoIFFBYF@SnVXKZl@aaaMQuCshqWwe8epHRYKnzQQBqEXXJGf5hKxSwuUcZieWDJRYLvdVKsd5Pw9NVR/BWy69uoCtfPi2Z2bVcPjEv6Iahurr7aMZDo1X6Ol0KvlCVJbX1MczuE@6n8Z7cOEWunOrPZeDu/IQc1/18zQS5VW7yqC3p4KAtXtW5/V50qNn7XzlU4t1OWsVqx9Yp1i3Ye2G6cd7pPZDq8c3JLDW9/FTlDnTud1asC4ISmX@Im3g34/Mbei7G3hpLs0Ijf6EmzuLoTNeRotHFgpw3mMRbakMAh8mjXRrPCssa1r@GJOarUlMbKSJjPgF "PHP – Try It Online")
Input sequence is sent using command arguments (each number is one argument) and output is string representation of arrays using PHP's [print\_r](https://www.php.net/manual/en/function.print-r.php).
```
for(;                  // outer loop on $argv input items
  ++$i<                // $i is current index, starts from second item as PHP fills first item with script's name
  $c=count($argv);     // $c is total number of input items
)
  for(                 // inner loop on input items
    $j=$i;             // $j is inner loop's index, always starts from outer loop's index
    $c>$j;             // continue while inner index is less than $c
    $j-$i-$n?:         // THIS IS RUN LAST: if current number ($n) is equal to $j-$i ($j is incremented by one already, so $j-$i gives a position starting from 1 in the $$i)
 $c=print\_r($$i) // THIS IS RUN LAST: print the $$i array and set $c to 1 to break the inner loop
 )
 $$i[]=             // THIS IS RUN FIRST: push into an array named by value of $i (if $i=2, variable name is 2)
    $n=$argv[$j++];    // THIS IS RUN FIRST: current number in index of inner loop and increment the index ($j) by one, $n also keeps current number
```
[Answer]
# [Red](http://www.red-lang.org), ~~149~~ 121 bytes
```
func[b][j: i: 0 until[if(i: i + 1)> n: length? b[j: j + 1 i: 1]if
b/(j + i) = i[print[copy/part at b j + 1 i]i: n]j = n]]
```
[Try it online!](https://tio.run/##ZVDLboMwELzzFaOcElVRvBiDjdT2H3q1fAhJaI0iFyFy6NfTMWlPCAt757VrT7fr8nG7@lD0LZb@kS6@C35oEVsoPNIc7z72e1YRL5DDG1KL@y19zl/v6LJwyHiWS4h90Z32GYgHvCL6cYpp9pfv8ec0nqcZ5xndvyHQksJAXQph6eEb1DCooFEyC6sXu@PxuCvIOgWr0KgNIZRr2syWUYp3KNfveao2GlOhtvT/7RuecAURg7JGyREMCwsHUQ52JSuLhmip4Rw0ZzFwFsKBamgF0dCMzz6Xu/BPqUgJQ6PLq3qGcjMCJyxq8DF0zjEMbIjo3MrK2oNj6PwSyy8 "Red – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 98 bytes
```
lambda s,e=enumerate:[s[i-n:i]for i,n in e([9]+s)if all(m+~j for j,m in e(s[i-n:i-1]))>0<n<=i]
```
[Try it online!](https://tio.run/##VVBBboMwELz3FXsExZG8GAcchX6EcqCqUR2BEwE59NKv012bIllisT0zOzva58/6/fBqG5qPbeynz68eFmEb61@TnfvVXtuldWd/dd3wmMEJD86DzVrTnZbcDdCPYzadfu/A9F1Mkd57ztjl@bu8@Vvjuu05O79mQ9ZWAi4CtIBSgBJQCGDd28EbKaCmqmQCY5Cq0KZTRpJaBjp@x7NMdMlDk82lDobHNREwSIVISQsKXHAszQBJDZ2SfvWuKgmrmCzIzxChOCsBhgjkxOSgyAGJVzwzOJk4nE9uRKQuzVYmVvk/jy@aNmCQAfLiJapor3lUxbiKYWrcM3BitS9y@wM "Python 3 – Try It Online")
Takes the input as a list and maps it to a list of lists where each item is a sublist of length `n` ending with item `n` for each item. Then it is filtered so that no list contains other valid length terminators.
-4 bytes thanks to Jonathan Frech
[Answer]
# [Zsh](https://www.zsh.org/),  ~~35~~ 37 bytes
*+2 bytes to suppress zero-length lists*
```
for x;((s=++i-x+1,x*s>0))&&<<<$@[s,i]
```
[Try it online!](https://tio.run/##qyrO@J@moanxPy2/SKHCWkOj2FZbO1O3QttQp0Kr2M5AU1NNzcbGRsUhulgnM/a/JleagiEQGimYKRhDSJCIgYGCAZAHghCWyX8A "Zsh – Try It Online")
For each element in the input, calculate the index of where the sequence would have to start. If the start index and the element are both `>0`, then print the elements from there to the current index, inclusive.
[Answer]
# Ruby 2.7-preview1, 50 bytes
Uses Ruby 2.7's new [`Enumerable#filter_map`](https://codegolf.stackexchange.com/a/187015/11261) method. (Sorry for no TiO link; TiO is still on Ruby 2.5.5.)
```
->a{i=0
a.filter_map{|c|i+=1
c>0&&c<=i&&a[i-c,c]}
```
[Answer]
# [Icon](https://github.com/gtownsend/icon), 121 bytes
```
procedure f(b)
r:=[];i:=j:=0
while j<*b do{if*b<(i+:=1)then j+:=1&i:=0 else b[j+i]=i&put(r,b[j+1+:i])&i:=*b}
return r
end
```
[Try it online!](https://tio.run/##rVLBjoIwEL3zFaMHA9pDS6lA1/6Bf2A4LFhjiaKpuGZj/HZ3prjGs5FQOn0zffP6qGsO3f1@9IfGrs/ewiauk8hrs6q@nDatNjy6bN3OQruY1rA@XN1mWi9iN9NGJP3WdtBSOMFiDnZ3slCv2pmrjJscz33sGS3FTLsqoZppfYu87c@@Ax/Zbv3Sef/tujiJAJ@lNpt4lTOYM1AMMgaSQcpAVEPe/lj/C402oyVM4OJdb@MEI9Q9alAkXAN2wvYwhnFyC7sedS8dRGCVoYP6MLdCznkR2J/huy3gVTPnDHgQPrzPZfYJepKKQwj0PUX7U@QuFAF4gBJnjp/iUZUhllMyxVOWmJBkJwIlJgSZigwSGQTmJTkRmMrBEpppoxC4SxFVOYzsvx8FCn9SKQhALroScqBX1ConXA5iCvHQQIoRy/n7ftDN/AM "Icon – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 64 bytes
```
->a,*s{(r=0;a.find{|x|(x==r+=1)&&s<<a[0,x]};w,*a=a)while a[0];s}
```
[Try it online!](https://tio.run/##PVDRasMwDHzPV@hhlLbzhhXHTUzq/UgIw6PJVihlJNua0fbbs5PdFaxYupPuFA/fb79z7@enl6DW43k5eF2H535/3J0v02U5eT88el4tFuN2GxqtpvZan9Q6@LA6fewPHQFs6/E6p@q9@xozok/qm@4nHOjhtc26425uSkUbRVZRocgoyhVxmzVOK6oQpUbBETaxxUqtwegIpnMvC7A4Fo2bKo7cU8CSIpjhlsM0FxMrABocbo1PdesqgJVC5lBxIIzsAMCBYNkECgYKDN6IU1RyyVJuGWTGlBUpl6L495PE4s8cCwAteQiT5K1YlYKbtEzFtx1kYxOf5Q8 "Ruby – Try It Online")
[Answer]
# [Prolog (SWI)](http://www.swi-prolog.org), 135 bytes
```
A*B:-reverse(A,B).
[]-_. [A|Q]-[A|R]:-Q-R.
+[Q|R]:-length([Q|R],Q),\+((R*Z,X-Z,S*X,+S)).
Q/R:-N-Q,N*Z,X-Z,R*X,+X.
X//Y:-bagof(R,X/R,Y).
```
[Try it online!](https://tio.run/##vVPLbptAFN3zFVZWM/YZYHgZiFrJ@QBLwAZnOqrSiDqWKKCB1Jv8u3sHYvW5aCu1At33PedwJQbTt/1RjOfT5bJb3@XCNJ8bMzZshzvuOkqL9@5K7V4KLciWOheFKF1no4o5aZvuOD2xOUPB8W7DWLm@Ry3uUa1rbCpOKIVX5mIvCuxfW6Vt1a5Te94hFx8ejv1HVqL2Shy4e3lq2qExTO1fKs1zwQZz6ia25zib09Swm5V4u7rhYHvaxtibiR1wGPkSVja8rd6oru@fp@F50hwLwGNvTPM48dslPZu@O3KOrnUvSiJAiAix9rzKdT49DO1pnNiiBCul1BYJYpoIaVJqXHOK5ip56pCVmozKfKQ@tr4Gu@rgtv6V5zopfR8@Ve2zRBbTn7GCb@z3Pbv5I3YcIUkJ/dVbZT9Fdo7CCFLGCBIEpDOmJEUG6WdI52aUYkvVIESWISTFMbIUkmQnCH3IECHR2L3MspGlUSkDxLSY2TdaQMnFEpmkJAFdLLQ4MQFuqRJaqlTOHCQjnM@l/kCOveH/V25Z/57PfuHvbVqef3lxwv/ljtaaflnnCw "Prolog (SWI) – Try It Online")
`A*B` is `reverse(A,B)`
`A-B` is true if A is a prefix of B
`+A` is true if A is a valid encoding, i.e. length-terminated list
`A/B` is true if, given list A, B is contained within A and B is a valid encoding
`A//B` is true if, given list A, Y is the list of all valid encodings in A (if there are no valid encodings in A, `A//B` fails)
Calling predicate `///2` with the left argument a list and the right argument a variable unifies the variable with the required result. For example, `[7,6,5,4,3,2,1]S.` will unify S with the `[[7, 6, 5, 4], [5, 4, 3], [3, 2], [1]]`. The TIO link contains a helper predicate that runs all inputs with against this predicate.
### Verbose
```
prefix([],_).
prefix(_,[]):-1<0.
prefix([A|Q], [A|R]):-
	prefix(Q,R).
suffix(A, B):-
	reverse(B, Q),
	prefix(P, Q),
	reverse(A, P).
encoded([H|T]):-
	length([H|T], H),
	\+((suffix(P,T), encoded(P))).
f(Q, R):-
	prefix(N, Q),
	suffix(R, N),
	reverse(R, T),
	encoded(T).
g(List, All):-
	bagof(Encoding, f(List, Encoding), All).
```
[Try it online!](https://tio.run/##TU7LaoNAFF03X3GXM/REah4NlG4MBLIIQcVNmYrkMTMVRIOTPhb5dzujY8jqct730jZVo6fmt@y6SytV@cdEjoIHE48KiJy/TcP3lzsloluSg@xJnTR58nyC1ObMt3IgAq17sZU/sjWSrUEJx90cezjK1h/btKxPzVmemdjesqG8krW@fg0EaOsyn8@M@ZkYGQeNqZhz26HsJ5Q@frb3Yz6UgvaP2xZnDo81mS3RbFeaKyiqqr7peNCNYhvnKGsNUl4fGT5Yg67TTISYYY4FlnnPFtKbTF8sVqBX0BK0AM1BM1BojR88@Ac "Prolog (SWI) – Try It Online")
The tricks used from this verbose solution is to first create another predicate for `reverse` because it is repeated many times. Then operators are used in place of predicate names. The predicate for suffix turns out to only be used once, so it can be removed, replacing its call in `f` with its conditions. Doing this removes the need to use `reverse(R, T)` as the suffix predicate already has the same condition.
[Answer]
# [T-SQL](https://www.microsoft.com/en-in/sql-server/sql-server-2019), 175 bytes
```
with s(r,i)as(select row_number()over(order by(rand())),*from string_split(<Comma delimited integer string>,','))select string_agg(l.i,',')from s,s l where l.r>s.r-s.i and l.r<=s.r and s.r-s.i>=0group by s.i
```
[Try it online!](https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=cf7c70794e819fa77afbe78dae66c89c)
---
**Verbose version**
```
with s(r,i) as
(
    select row_number() over (order by (rand())) -- Add a row number for sorting
          ,*
    from string_split('7,6,5,4,3,2,1',',')       -- to the split out integer list
)
select string_agg(l.i,',')                       -- then aggregate back together
from s,s l                                       -- joining to itself
where l.r > s.r - s.i                            --   where the previous numbers sit in rows between [current int value] rows before
    and l.r <= s.r                               --     and the current row
    and s.r - s.i >= 0                           --     and there are enough list items for the current integer value
group by s.i                                     -- grouped to each output string
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 12 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ŒʒDη.Δ¤sgQ}Q
```
[Try it online](https://tio.run/##yy9OTMpM/f//6KRTk1zObdc7N@XQkuL0wNrA//@jDXUUjHQUjHUUTHQUTGMB) or [verify all test cases](https://tio.run/##LU8xbgIxEOx5Bbp6FHm9Xp83TRoaqoj6RAFSFKVJkA4ipeATiCfwhtAj0aTgS8f4iLzyzszuztpf/Wr98TZ8N/PPzW77PG3wg0nzutuS9SMdroe/w@x2frodL6f@fbFfDHtcfl@GrmuRYUhQRMgSnQeUgDYQCiVlySoOAYG8ngdKVBmWkAvb/jMVggQRQ8yIdDOSAocERxmLqaClGhXuUC4xeIFwU4YGiEJpWOe8@vJmq0iEcdBrpIcpkwlcSDL4E60@RsOWitZVRcYdfIbWTy3v).
**Explanation:**
```
Œ           # Get all sublists of the (implicit) input-list
 ʒ          # Filter it by:
  D         #  Duplicate the current sublist
   η        #  Pop the copy and push its prefixes
    .Δ      #  Find the first prefix that's truthy for:
            #  (or -1 if none are)
      ¤     #   Push the last value (without popping the list itself)
       s    #   Swap so the list is at the top
        g   #   Pop and push its length
         Q  #   Check if the two values are the same
     }Q     #  After the find_first: Check if both are the same
            # (after which the filtered list is output implicitly)
```
The `¤sgQ` could alternatively be `DgÅ¿` or `Ðθ∍Q` for the same byte-count.
```
      D     #   Duplicate the list
       g    #   Pop the copy and push its length
        Å¿  #   Check if the list ends with this length as trailing value
      Ð     #   Triplicate the list
       θ    #   Pop one copy and push its last value
        ∍   #   Extend/shorten another copy to this length
         Q  #   Check if the two lists are still the same
```
[Answer]
# APL(NARS), chars 148, bytes 296
```
r←f w;i;j;v;q
k←≢w⋄r←⍬⋄i←0⋄w←(⍳k),¨w
→4×⍳k<i+←1⋄→2×⍳(j>q←≢v←i↑w)∨0=j←2⊃i⊃w⋄v←v↓⍨q-j⋄→3×⍳r≡⍬⋄→2×⍳∨/{(⍵[1]≡v[1])∧⍵⊆v}¨r
r←r,⊂v⋄→2
→0×⍳r≡⍬⋄r←{⍵[2]}¨¨r
```
because of the problem one has to know index of the element, the input set is elaborate as one set with indices (`w←(⍳k),¨w`) that in the end were removed (in `r←{⍵[2]}¨¨r`) test:
```
  ⎕fmt f 90 80 70
┌0─┐
│ 0│
└~─┘
  ⎕fmt f 100 0  2  2  2 0 0 2 4
┌5─────────────────────────────────────┐
│┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌4───────┐│
││ 0 2│ │ 2 2│ │ 2 2│ │ 0 2│ │ 0 0 2 4││
│└~───┘ └~───┘ └~───┘ └~───┘ └~───────┘2
└∊─────────────────────────────────────┘
  ⎕fmt f 7 6 5 4 3 2 1
┌4──────────────────────────────┐
│┌4───────┐ ┌3─────┐ ┌2───┐ ┌1─┐│
││ 7 6 5 4│ │ 5 4 3│ │ 3 2│ │ 1││
│└~───────┘ └~─────┘ └~───┘ └~─┘2
└∊──────────────────────────────┘
  ⎕fmt f 1 2 3 4 5
┌1───┐
│┌1─┐│
││ 1││
│└~─┘2
└∊───┘
  ⎕fmt f ⍬
┌0─┐
│ 0│
└~─┘
```
]