4

APL (Dyalog Extended), 60 bytesSBCS {(⍺/⊃¨⍵)@((⍺×≢⍵)?+/,1=n)@{1=n}'@# '[n←(0⍺,⍺+1)⍸⍤1|∘.⌾⍨⍺…-⍺]} Try it online!


Only top voted, non community-wiki answers of a minimum length are eligible