DEV Community

Saranya R
Saranya R

Posted on

Next Permutation

Approach Explanation (o゜▽゜)o☆

  1. I scanned from right to left to find the first element that breaks the descending order using the first while loop.
  2. I looked for the smallest element greater than the pivot on its right.
  3. I swapped the pivot and successor.
  4. I reversed the part of the array after the pivot to get the next lexicographical order.
  5. If no pivot exists, I reversed the whole array to get the lowest order.

Method Used: (/^▽^)/

  • Right-to-left scan.
  • Swapping.
  • Reverse suffix.
  • In-place rearrangement.
  • extra space.
  • Single pass logic.

Top comments (0)