1 of 3

MATL, 7 bytes

3X2Z%Bz

Try it online!

Exact transliteration of my MATLAB answer. Note that input and output are implicit.