Pregunta de entrevista de Squarepoint Capital

Hackerrank question is medium difficulty