Problem 1053 · Medium

Previous Permutation With One Swap

Find the largest arrangement that is lexicographically smaller than the given one using at most one swap. The scan finds the rightmost place where a smaller digit can be swapped in.

Open the interactive visualisation