Math Fundamentals18 sections · 814 units
Open in Course

Practice - Next Permutation

(LeetCode 31)

Given an array representing a permutation, find the next lexicographically larger permutation in-place.

Example: [1,2,3] → [1,3,2]. [3,2,1] → [1,2,3] (wrap around).

Hint: Find the first decreasing element from the right, swap it with the smallest larger element to its right, then reverse the suffix.