Pregunta de entrevista de Flatiron Health

Design an algorithm to solve this problem.