Skip to main content
1 of 4
user
  • 387
  • 2
  • 21
  • 68

APL (Dyalog Unicode), 35 bytes

{1≥|⊃⍵-(+/÷≢)⍵:0⋄1+∇h,3÷⍨3+/⍵,h←⊃⍵}

Try it online!

A recursive function.

user
  • 387
  • 2
  • 21
  • 68