# [Haskell], 71 bytes <!-- language-all: lang-hs --> [x|x<-[0..],0#x] a#x=a!divMod x 10 a!(d,m)|d<1=a==m|1>0=m<=a&&(a-m+1)#d [Try it online!][TIO-kx3xa1xk] [Haskell]: https://www.haskell.org/ [TIO-kx3xa1xk]: https://tio.run/##BcFLCoMwEADQvacYCYhSI8k@0xt4AitlMP2EZGyopWSRs3f63pOOeEtJIl5kKbU4vZhpWkejytqQKkitD9/55aGANQ21vR95qN5ZJESu9myQHVLX9aT5ZAflhSnsgMCU5yvkd9g/EOW33RM9DtFbzn8 "Haskell – Try It Online" - _k_ is an infinite sequence We start from the end checking if the last digit(m) doesn't consume more groups than available `a` _a-m>=0_ Then we remove `m` groups and add 1 _a=a-m+1_ and move backwards. At the end we must have exactly one group _a-m+1==1_ <pre> 3010200 m a a=a-m+1 0 0 1 0 1 2 2 2 1 0 1 2 1 2 2 0 2 3 3 3 1 </pre>