Next Permutation - Problem

Imagine you have a sequence of numbers, like a combination lock or a digital counter. Given the current arrangement, you need to find the very next arrangement in lexicographical (dictionary) order.

A permutation is simply a rearrangement of elements. For example, [1,2,3] can be rearranged as [1,3,2], [2,1,3], [2,3,1], [3,1,2], or [3,2,1].

The Challenge: Given an array of integers, find the next lexicographically greater permutation. If no such permutation exists (you're at the highest possible arrangement), wrap around to the smallest permutation (sorted in ascending order).

Examples:

  • [1,2,3][1,3,2] (next in dictionary order)
  • [2,3,1][3,1,2] (jump to next valid arrangement)
  • [3,2,1][1,2,3] (wrap around to smallest)

Constraints: You must modify the array in-place using only constant extra memory!

Input & Output

example_1.py — Basic Next Permutation
$ Input: [1,2,3]
Output: [1,3,2]
💡 Note: The next lexicographical permutation of [1,2,3] is [1,3,2]. We find that position 1 (value 2) can be incremented, swap with 3, giving us [1,3,2].
example_2.py — Jump to Next Valid
$ Input: [3,2,1]
Output: [1,2,3]
💡 Note: This is the highest possible permutation, so we wrap around to the lowest permutation [1,2,3]. The algorithm detects no valid pivot and reverses the entire array.
example_3.py — Complex Case
$ Input: [1,1,5]
Output: [1,5,1]
💡 Note: Even with duplicate values, the algorithm works correctly. Position 1 (value 1) can be incremented to 5, then we reverse the suffix [1] which remains [1].

Visualization

Tap to expand
🚗 Digital Counter AnalogyCurrent: [2, 3, 1]231Can increment!3 > 1 (descending)Next: [3, 1, 2]312Incremented 2→3Reversed [3,1]→[1,2]Algorithm Steps:1Find rightmost nums[i] < nums[i+1] → position 0 (value 2)2Find rightmost nums[j] > nums[i] → position 1 (value 3)3Swap nums[0] and nums[1] → [3, 2, 1]4Reverse suffix [2, 1] → [1, 2] → Final: [3, 1, 2]Optimal Solution⏱️ Time: O(n)💾 Space: O(1)Next!
Understanding the Visualization
1
Find Increment Position
Like finding which digit in 1923 can still be incremented (digit 2 can become 3)
2
Find Next Value
Find the smallest value greater than current digit (next valid digit)
3
Increment
Replace with the next valid value (like 2 → 3)
4
Reset Suffix
Make all positions to the right as small as possible (like resetting to 00)
Key Takeaway
🎯 Key Insight: Just like incrementing a digital counter, we only need to change the minimal suffix to get the next valid arrangement - find the rightmost 'digit' that can be incremented, increment it, then reset everything to its right to the minimum value!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass to find pivot, single pass to find swap target, single pass to reverse suffix

n
2n
Linear Growth
Space Complexity
O(1)

Only uses a few extra variables, modifies array in-place

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 100
  • Must modify the array in-place
  • Use only O(1) extra memory space
Asked in
Google 42 Meta 38 Amazon 35 Microsoft 28
78.4K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen