Pregunta de entrevista de Jane Street

create algorithm and it's time complexity