Problems
mediumNext Permutation
5 Koda Coins

Next Permutation

medium2s limit💾 64 MB

Description

Implement the next permutation algorithm, which rearranges numbers into the lexicographically next greater permutation of numbers. If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).


Input Format

  • First line: Integer N.
  • Second line: N space-separated integers.

  • Output Format

  • Print the next permutation space-separated.

  • Constraints

  • 1 <= N <= 100
  • 0 <= A[i] <= 100