Pergunta de entrevista da empresa Arm

Logical puzzle. Took a while to solve