Pregunta de entrevista de J.P. Morgan

Next permutation problem was asked.