J.P. Morgan Interview Question

Next permutation problem was asked.