Revision 00a7c02d-f3d6-4c8d-b8e5-ec644814ee8f - Code Golf Stack Exchange
# [Jelly], 36 [bytes]
3Ŀḟ0ṠE
ÑṠ¬Ȧ¬×ÑṠḟ0Ṃ
ÑAI
OIḟ0
ÇṠḢ;2Ŀ;Ñ
**[TryItOnline]**
Or see the [test cases]
(My lack of chaining skill is probably costing a few bytes here)
[Jelly]: https://github.com/DennisMitchell/jelly
[bytes]: https://github.com/DennisMitchell/jelly/wiki/Code-page
[TryItOnline]: http://jelly.tryitonline.net/#code=M8S_4bifMOG5oEUKw5HhuaDCrMimwqzDl8OR4bmg4bifMOG5ggrDkUFJCk9J4bifMArDh-G5oOG4ojsyxL87w5E&input=&args=aXJpcw
[test cases]: http://jelly.tryitonline.net/#code=M8S_4bifMOG5oEUKw5HhuaDCrMimwqzDl8OR4bmg4bifMOG5ggrDkUFJCk9J4bifMArDh-G5oOG4ojsyxL87McS_CsW8w4figqxZ4bmE4oKseDA&input=&args=WyJlYXJ0aCIsImdyb3VwIiwib3BlcmEiLCJzdGF5IiwiaXJpcyIsIm1pbmVyIiwidGF4IiwicGx1ZyIsIm1ldGEiLCJheGlvbSIsImF4aXMiLCJjb29rIiwid2F0Y2giLCJ5ZXRpIiwicGlsbCIsIm1vbSIsImRhZCJd