# [J], <s><sup>29</sup></s> 23 bytes

    1{(,~#|1+}.i.{.)@]^:[&0

[Try it online!][TIO-jwqxklbe]

[J]: http://jsoftware.com/
[TIO-jwqxklbe]: https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/Das1dOqUawy1a/Uy9ar1NB1i46yi1Qz@a3KlJmfkAxXaKqQpGEI4RmCOBYRjCpGxhPAswTwjcwjPBKLS1AjZEDOT/wA "J – Try It Online"