0
0
DSA Pythonprogramming

Next Permutation of Array in DSA Python

Choose your learning style9 modes available
Mental Model
Find the next bigger arrangement of numbers by changing the smallest possible part of the array.
Analogy: Imagine you have a lock with digits. To get the next code, you try to increase the last digit. If it can't go higher, you move left to find a digit you can increase, then reset digits to the right to the smallest order.
Array: [1, 2, 3, 4, 5]
Indexes:  0  1  2  3  4
We want to find the next bigger order by changing digits from right to left.
Dry Run Walkthrough
Input: array: [1, 2, 3]
Goal: Find the next permutation which is just bigger than [1, 2, 3]
Step 1: Find the first index from right where array[i] < array[i+1], here i=1 (2 < 3)
[1, 2, 3] ↑i=1
Why: We need to find where the order breaks from the right to find the pivot to increase
Step 2: Find the first index from right where array[j] > array[i], here j=2 (3 > 2)
[1, 2, 3] ↑j=2
Why: We find the number just bigger than array[i] to swap and get next bigger permutation
Step 3: Swap array[i] and array[j], array becomes [1, 3, 2]
[1, 3, 2]
Why: Swapping increases the number at i to the next bigger digit
Step 4: Reverse the subarray after i (index 2 to end), here only one element so no change
[1, 3, 2]
Why: Reversing makes the tail the smallest possible order to get the next permutation
Result:
[1, 3, 2]
Annotated Code
DSA Python
from typing import List

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        n = len(nums)
        i = n - 2
        # Find first decreasing element from right
        while i >= 0 and nums[i] >= nums[i + 1]:
            i -= 1
        if i >= 0:
            j = n - 1
            # Find element just bigger than nums[i]
            while nums[j] <= nums[i]:
                j -= 1
            nums[i], nums[j] = nums[j], nums[i]  # Swap
        # Reverse the tail
        left, right = i + 1, n - 1
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1
            right -= 1

if __name__ == '__main__':
    arr = [1, 2, 3]
    Solution().nextPermutation(arr)
    print(arr)
while i >= 0 and nums[i] >= nums[i + 1]: i -= 1
Find the first index from right where order breaks (nums[i] < nums[i+1])
if i >= 0:
Check if such index exists, meaning next permutation is possible
while nums[j] <= nums[i]: j -= 1
Find the element just bigger than nums[i] from right
nums[i], nums[j] = nums[j], nums[i]
Swap to increase the number at i
while left < right: nums[left], nums[right] = nums[right], nums[left] left += 1 right -= 1
Reverse the subarray after i to get the smallest tail order
OutputSuccess
[1, 3, 2]
Complexity Analysis
Time: O(n) because we scan the array from right to left a few times but only once fully
Space: O(1) because we modify the array in place without extra space
vs Alternative: Naive approach would generate all permutations and find the next one, which is O(n!) and very inefficient
Edge Cases
array is in descending order like [3, 2, 1]
No next bigger permutation exists, so array is reversed to smallest order [1, 2, 3]
DSA Python
while i >= 0 and nums[i] >= nums[i + 1]:
    i -= 1
array has only one element like [1]
No change since only one permutation exists
DSA Python
while i >= 0 and nums[i] >= nums[i + 1]:
    i -= 1
array has repeated elements like [1, 5, 5]
Next permutation is found correctly by swapping and reversing tail
DSA Python
while nums[j] <= nums[i]:
    j -= 1
When to Use This Pattern
When you need to find the next bigger arrangement of numbers or characters in order, use the next permutation pattern because it efficiently finds the next lexicographical order.
Common Mistakes
Mistake: Not reversing the tail after swapping, leading to wrong next permutation
Fix: Always reverse the subarray after the swapped index to get the smallest tail order
Mistake: Swapping the wrong elements or not finding the correct pivot index
Fix: Carefully find the first decreasing element from right and then find the just bigger element to swap
Summary
Finds the next lexicographically bigger permutation of the array in place.
Use when you want to generate permutations in order or find the next bigger number with same digits.
The key is to find the pivot from right where order breaks, swap with just bigger element, then reverse the tail.