A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
arr = [1,2,3] is [1,3,2].arr = [2,3,1] is [3,1,2].arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.Given an array of integers nums, find the next permutation of nums.
The replacement must be in place and use only constant extra memory.
Example 1:
Input: nums = [1,2,3] Output: [1,3,2]
Example 2:
Input: nums = [3,2,1] Output: [1,2,3]
Example 3:
Input: nums = [1,1,5] Output: [1,5,1]
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are holding a sequence of items and need to find the exact configuration that comes immediately after your current one in a complete dictionary of all possible arrangements. The brute force approach involves generating every single valid way to arrange your items, sorting that massive list from first to last, and simply looking up the arrangement that sits directly after the one you currently have.
Here's how the algorithm would work step-by-step:
import itertools
def next_permutation(sequence_items):
# Generate all unique permutations so we can examine every possible arrangement
all_permutations = list(itertools.permutations(sequence_items))
unique_permutations = sorted(list(set(all_permutations)))
# Locate our starting position to find the exact relative order in the dictionary
current_index = unique_permutations.index(tuple(sequence_items))
next_index = (current_index + 1) % len(unique_permutations)
# Mutate the input list in-place to satisfy the standard interview problem format
sequence_items[:] = list(unique_permutations[next_index])Instead of scrambling all the items randomly to find the next arrangement, we can find the next logical step by looking for a small backward shift from right to left. By making a tiny adjustment near the end and tidying up the rest, we instantly reach the very next arrangement in the sequence.
Here's how the algorithm would work step-by-step:
def next_permutation(permutation_list): # Scan backward to find the first element that violates the non-decreasing order. pivot_index = len(permutation_list) - 2 while pivot_index >= 0 and permutation_list[pivot_index] >= permutation_list[pivot_index + 1]: pivot_index -= 1 if pivot_index >= 0: # Find the smallest element to the right of the pivot that is larger than the pivot. successor_index = len(permutation_list) - 1 while permutation_list[successor_index] <= permutation_list[pivot_index]: successor_index -= 1 permutation_list[pivot_index], permutation_list[successor_index] = permutation_list[successor_index], permutation_list[pivot_index] # Reverse the suffix to ensure the smallest possible lexicographical order for the remainder. left_pointer = pivot_index + 1 right_pointer = len(permutation_list) - 1 while left_pointer < right_pointer: permutation_list[left_pointer], permutation_list[right_pointer] = permutation_list[right_pointer], permutation_list[left_pointer] left_pointer += 1 right_pointer -= 1| Case | How to Handle |
|---|---|
| Array of size 0 or 1 | The algorithm should return immediately because a single element or empty array has no next permutation. |
| Array sorted in strictly descending order | Since no smaller element can be found from the right, the entire array is simply reversed to become the lowest possible order. |
| Array sorted in strictly ascending order | The algorithm identifies the second to last element as the pivot, swaps it with the last element, and maintains the sorted order. |
| Array containing duplicate elements | The scan from the right correctly finds the first decreasing element even with duplicates, treating equal values properly. |
| Array containing negative numbers and zeros | Lexicographical ordering applies naturally to negative integers, zeros, and positive integers without requiring special transformations. |
| Maximum size input array reaching constraints | The linear time complexity approach guarantees efficient in-place execution well within time limits without memory overhead. |
| Array with all identical elements | The algorithm treats it as non-increasing from left to right, reversing the entire array to leave it unchanged. |
| Array of size 2 with elements in ascending order | The two elements are simply swapped to produce the only other possible permutation. |