Skip to main content
2 of 9
added 202 characters in body
Leaky Nun
  • 50.1k
  • 6
  • 110
  • 284

#J, 69 chars#

(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*]))

Try it online!

Sample input:

(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*])) 8
12

Notes:

Too many exceptions to handle...

Leaky Nun
  • 50.1k
  • 6
  • 110
  • 284