Skip to main content
1 of 7

Jelly, 9 bytes

ŒPḊƇPƇ§Qf

Try it online!