J.P. Morgan interview question

Next permutation problem was asked.