Next Permutation of Array in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to find the next permutation changes as the array size grows.
Specifically, how many steps does the algorithm take when the array gets bigger?
Analyze the time complexity of the following code snippet.
function nextPermutation(nums: number[]): void {
let i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
if (i >= 0) {
let j = nums.length - 1;
while (nums[j] <= nums[i]) j--;
[nums[i], nums[j]] = [nums[j], nums[i]];
}
reverse(nums, i + 1);
}
function reverse(nums: number[], start: number): void {
let left = start, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
This code finds the next lexicographical permutation of the array in place.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Three loops: one to find the pivot, one to find the swap partner, and one to reverse part of the array.
- How many times: Each loop can run up to n times, where n is the array length.
As the array size grows, each loop may scan a portion of the array once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to about 30 steps (3 loops x 10 steps each) |
| 100 | Up to about 300 steps |
| 1000 | Up to about 3000 steps |
Pattern observation: The total steps grow roughly in a straight line with the input size.
Time Complexity: O(n)
This means the time to find the next permutation grows linearly with the array size.
[X] Wrong: "The algorithm must check all permutations, so it takes factorial time."
[OK] Correct: The algorithm smartly finds the next permutation by scanning the array only a few times, not generating all permutations.
Understanding this linear time approach shows you can handle array manipulation efficiently, a skill often valued in coding challenges.
"What if we used a sorting step instead of reversing the suffix? How would the time complexity change?"
Practice
next permutation of an array represent?Solution
Step 1: Understand permutation order
Permutations can be ordered lexicographically like words in a dictionary.Step 2: Define next permutation
The next permutation is the immediate next arrangement that is greater than the current one.Final Answer:
The next lexicographically greater arrangement of the array elements -> Option DQuick Check:
Next permutation means next bigger order [OK]
- Thinking next permutation is just sorting ascending
- Confusing next permutation with reversing the array
- Assuming next permutation always moves smallest element front
Solution
Step 1: Check parameter type
The input should be an array of numbers, sonums: number[]is correct.Step 2: Check return type
The function modifies the array in place and returns nothing, sovoidis correct.Final Answer:
function nextPermutation(nums: number[]): void -> Option AQuick Check:
Input array, no return needed [OK]
- Using string type instead of number array
- Returning a new array instead of modifying in place
- Using incorrect parameter types
[1, 3, 2]?Solution
Step 1: Find the pivot index i
Start from i = nums.length - 2 = 1. nums[1] = 3 >= nums[2] = 2, so i-- to 0. nums[0] = 1 < nums[1] = 3, pivot i = 0.Step 2: Find successor j
From j = nums.length - 1 = 2, find rightmost j > i with nums[j] > nums[i]. j=2: 2 > 1, yes.Step 3: Swap pivot and successor
Swap nums[0] and nums[2]: [2, 3, 1].Step 4: Reverse suffix after i
Reverse indices 1 to 2: [3, 1] becomes [1, 3]. Final: [2, 1, 3].Final Answer:
[2, 1, 3] -> Option AQuick Check:
Next permutation of [1,3,2] is [2,1,3] [OK]
- Swapping wrong elements
- Not reversing suffix after swap
- Confusing next permutation with sorting
function nextPermutation(nums: number[]): void {
let i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
let j = nums.length - 1;
while (nums[j] <= nums[i]) {
j--;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
}
nums.reverse();
}Solution
Step 1: Analyze reversal step
The code reverses the entire array withnums.reverse()instead of just the suffix after index i+1.Step 2: Understand correct reversal
Only the part of the array after the pivot index should be reversed to get the next permutation.Final Answer:
The entire array is reversed instead of just the suffix -> Option BQuick Check:
Reverse suffix, not whole array [OK]
- Reversing entire array instead of suffix
- Incorrect swap syntax
- Wrong loop conditions causing errors
[5, 4, 3, 2, 1]. After applying the next permutation algorithm, what will be the resulting array?Solution
Step 1: Check if array is highest permutation
The array is in descending order, which is the highest permutation.Step 2: Apply next permutation rule for highest permutation
If no next bigger permutation exists, the array is reversed to the lowest permutation.Final Answer:
[1, 2, 3, 4, 5] -> Option CQuick Check:
Descending array next permutation is ascending [OK]
- Returning same array instead of reversed
- Trying to swap elements incorrectly
- Confusing next permutation with partial sorting
Ready to prove it?
Pass the test and this pattern moves unknown
