Next Permutation of Array in DSA C# - Time & Space Complexity
We want to understand how the time needed to find the next permutation changes as the array size grows.
How does the number of steps grow when the array gets bigger?
Analyze the time complexity of the following code snippet.
public void NextPermutation(int[] nums) {
int i = nums.Length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
if (i >= 0) {
int j = nums.Length - 1;
while (nums[j] <= nums[i]) j--;
Swap(nums, i, j);
}
Reverse(nums, i + 1, nums.Length - 1);
}
private void Swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void Reverse(int[] nums, int start, int end) {
while (start < end) {
Swap(nums, start, end);
start++;
end--;
}
}
This code finds the next lexicographical permutation of the array in place.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loops that scan the array from the end to find the pivot and the element to swap, plus the reverse operation.
- How many times: Each loop can run up to n times in the worst case, where n is the array length.
As the array size grows, the number of steps grows roughly in a straight line with the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 to 30 steps |
| 100 | About 100 to 300 steps |
| 1000 | About 1000 to 3000 steps |
Pattern observation: The steps increase roughly in direct proportion to the array size.
Time Complexity: O(n)
This means the time to find the next permutation grows linearly with the size of the array.
[X] Wrong: "The algorithm checks every possible permutation, so it must be exponential time."
[OK] Correct: The algorithm only scans the array a few times and does swaps and reversals, so it runs in linear time, not exponential.
Understanding this linear time solution shows you can handle array manipulation and think about efficient scanning, which is a valuable skill in many coding problems.
"What if we used a sorting method instead of reversing the suffix? How would the time complexity change?"
Practice
Next Permutation algorithm do to an array of numbers?Solution
Step 1: Understand the purpose of Next Permutation
The algorithm finds the next arrangement of numbers that is just larger than the current one in dictionary order.Step 2: Compare with other options
Sorting or reversing changes the order but does not guarantee the next immediate permutation. Finding max element is unrelated.Final Answer:
It rearranges the array to the next lexicographically greater permutation. -> Option DQuick Check:
Next permutation = next lexicographically greater order [OK]
- Confusing next permutation with sorting
- Thinking it just reverses the array
- Assuming it finds max element
nums[i] and nums[j] in an array?Solution
Step 1: Check correct swap syntax in C#
Using a temporary variable to hold one value before swapping is the correct approach in C#.Step 2: Analyze other options
nums[i] = nums[j]; nums[j] = nums[i]; overwrites nums[i] before assigning to nums[j]. Swap(nums[i], nums[j]); is invalid without a defined method. nums[i], nums[j] = nums[j], nums[i]; is Python syntax, not C#.Final Answer:
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; -> Option AQuick Check:
Use temp variable to swap in C# [OK]
- Overwriting one element before saving it
- Using Python tuple swap syntax in C#
- Calling undefined Swap method
int[] nums = {1, 3, 2}?Solution
Step 1: Find the first decreasing element from the right
From right, 2 < 3 is false, but 3 > 1 is true, so pivot is at index 0 (value 1).Step 2: Find the element just larger than pivot to the right and swap
Element just larger than 1 on right is 2 at index 2. Swap 1 and 2: array becomes [2, 3, 1].Step 3: Reverse the suffix after pivot index
Reverse elements after index 0: [3, 1] reversed to [1, 3]. Final array: [2, 1, 3].Final Answer:
[2, 1, 3] -> Option CQuick Check:
Next permutation of [1,3,2] = [2,1,3] [OK]
- Not reversing suffix after swap
- Swapping with wrong element
- Confusing pivot position
void NextPermutation(int[] nums) {
int i = nums.Length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
if (i >= 0) {
int j = nums.Length - 1;
while (nums[j] <= nums[i]) j--;
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;
}
Array.Reverse(nums, i, nums.Length - i);
}Solution
Step 1: Analyze Array.Reverse parameters
Array.Reverse(nums, i, nums.Length - i) reverses from index i, but suffix to reverse starts at i+1.Step 2: Correct the start index for reversal
It should be Array.Reverse(nums, i + 1, nums.Length - (i + 1)) to reverse the suffix properly.Final Answer:
The Array.Reverse call has wrong start index; should be i + 1. -> Option AQuick Check:
Reverse suffix starting at i+1, not i [OK]
- Reversing from wrong index
- Ignoring empty array edge case
- Swapping wrong elements
nums = {5, 4, 3, 2, 1}. What will be the result after applying the Next Permutation algorithm once, and why?Solution
Step 1: Identify if array is highest permutation
The array is strictly descending, so it is the highest lexicographical permutation.Step 2: Apply rule for highest permutation
Next permutation resets to the lowest order by sorting ascending, resulting in [1, 2, 3, 4, 5].Final Answer:
[1, 2, 3, 4, 5] because the array is in descending order, so next permutation is the lowest ascending order. -> Option BQuick Check:
Descending array next permutation = ascending order [OK]
- Thinking highest permutation stays same
- Decreasing last element incorrectly
- Swapping wrong elements
Ready to prove it?
Pass the test and this pattern moves unknown
