Given an array of integers nums, rearrange it into the next lexicographically greater permutation. If no greater permutation exists (the array is sorted descending), rearrange to the smallest permutation (sorted ascending). You must do this in-place with extra space. Google commonly asks permutation problems to assess your pattern recognition and in-place array manipulation skills.
For nums = [1, 2, 3], the next permutation is [1, 3, 2]. For nums = [3, 2, 1], wrap around to [1, 2, 3].
Before reading the solution, consider: which position in the array should you change, and why?
Constraints: , values from to .