AlgoMaster Logo

Next Permutation

mediumFrequency5 min readUpdated June 23, 2026

Understanding the Problem

Given a sequence of numbers, rearrange them into the next sequence that would come after the current one if we listed all possible arrangements in dictionary order.

An odometer is a useful comparison. The number 1234 is followed by 1235, then 1236, and so on. When the last digit reaches its maximum, you increment the next digit and reset. Permutations work the same way, except instead of digits 0-9 you are working with the elements of the array, and instead of addition you are rearranging positions.

To get the next permutation, find the rightmost position where the sequence can still grow, make the smallest possible increase at that position, and arrange everything after it in the smallest possible order.

Key Constraints:

  • 1 <= nums.length <= 100 → With n up to 100, the input is small. The difficulty is correctness, not raw speed.
  • 0 <= nums[i] <= 100 → Values can repeat, so the algorithm has to handle duplicates like [1, 1, 5] correctly.
  • in place, constant extra memory → We cannot copy the array or use extra data structures. This rules out the brute-force option of materializing all permutations and searching for the next one.

Approach 1: Generate All Permutations (Brute Force)

Intuition

Generate every possible permutation of the array, sort them in lexicographic order, find the current permutation in the list, and return the one after it. If the current permutation is the last in the list, wrap around to the first.

This is correct by construction, but impractical. For an array of length n there are n! permutations. With n = 10 that is 3.6 million arrangements to generate and sort, and with n = 20 it is over 2 quintillion. It also stores every permutation, so it violates the constant-memory requirement.

It does make the structure of the problem visible: when the permutations are listed in order, the transitions follow a fixed pattern that the next approach exploits directly.

Algorithm

  1. Generate all permutations of the input array.
  2. Sort the permutations in lexicographic order.
  3. Find the current array in the sorted list.
  4. Return the next permutation in the list (wrapping around to the first if needed).

Example Walkthrough

Input:

0
1
1
2
2
3
nums

All permutations sorted: [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1]. Current is [1,2,3] at position 0. Next is position 1:

0
1
1
3
2
2
nums

Code

This approach is simple but unusable for larger inputs. The next approach computes the next permutation directly from the current one, in place, without enumerating anything.

Approach 2: Optimal (Single Pass with Suffix Reversal)

Intuition

When the permutations of [1, 2, 3, 4] are listed in order, the rightmost elements change most frequently, like the least-significant digits of a number. When a suffix is already in descending order it cannot grow any larger, so the increase has to carry over to an earlier position.

That gives the algorithm: scan from the right to find the first position where the sequence can still grow (the pivot), swap the pivot with the smallest element to its right that is larger than it, and reverse the suffix to make it the smallest possible arrangement.

Algorithm

  1. Start from the right end of the array. Find the first index i such that nums[i] < nums[i + 1]. This is the pivot.
  2. If no pivot exists (the array is in descending order), reverse the entire array and return.
  3. From the right end, find the first index j such that nums[j] > nums[i].
  4. Swap nums[i] and nums[j].
  5. Reverse the subarray from i + 1 to the end.

Example Walkthrough

1Initialize: scan right to left to find pivot
0
1
1
3
2
5
3
4
i
4
2
1/7

Code