Next Permutation

Rearrange to the lexicographically next greater permutation; wrap to lowest if impossible.

Medium
Time O(n) Space O(1)
LeetCode
4 min read
two-pointers array permutation

Problem#

In place, rearrange nums to the lexicographically next greater permutation. If the array is the largest permutation (descending), rearrange to the smallest (ascending).

Examples#

  • [1,2,3][1,3,2]
  • [3,2,1][1,2,3]
  • [1,1,5][1,5,1]

Constraints#

  • 1 <= nums.length <= 100.

Hints#

Hint 1
Find the first descending step from the right (the “pivot”). Swap it with the next larger value to its right, then reverse the suffix.

Approach#

  1. Scan from the right to find pivot i where nums[i] < nums[i+1]. If none, the array is fully descending — reverse to smallest.
  2. Else, scan from the right to find the smallest nums[j] > nums[i]. Swap them.
  3. Reverse nums[i+1..end] (it was descending; reversing makes it ascending, hence the smallest possible suffix).

Solution#

Next Permutation
class Solution {
public:
void nextPermutation(vector<int>& nums) {
const int n = nums.size();
int i = n - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) --i;
if (i >= 0) {
int j = n - 1;
while (nums[j] <= nums[i]) --j;
swap(nums[i], nums[j]);
}
reverse(nums.begin() + i + 1, nums.end());
}
};

Editorial#

The pivot i is the unique position where increasing changes the rank. Everything to its right is non-increasing, so the smallest possible value strictly greater than nums[i] in that suffix swaps in cleanly (the swap preserves the descending order of the suffix). Reversing the suffix then converts it to ascending — the smallest possible tail given the new prefix. If no pivot exists, the array is the largest permutation and we wrap to ascending.

Complexity#

  • Time: O(n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.