Challenge - 5 Problems
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the next permutation?
Given the array
[1, 3, 2], what will be the array after applying the next permutation algorithm once?DSA C#
int[] arr = {1, 3, 2}; NextPermutation(arr); Console.WriteLine(string.Join(", ", arr)); 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); } void Swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } void Reverse(int[] nums, int start, int end) { while (start < end) { Swap(nums, start, end); start++; end--; } }
Attempts:
2 left
💡 Hint
Look for the first pair from the right where the left number is smaller, then swap and reverse the tail.
✗ Incorrect
The next permutation finds the next lexicographically greater sequence. For [1,3,2], the next is [2,1,3].
❓ Predict Output
intermediateNext permutation of a descending array
What is the output after applying next permutation on the array
[3, 2, 1]?DSA C#
int[] arr = {3, 2, 1}; NextPermutation(arr); Console.WriteLine(string.Join(", ", arr)); 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); } void Swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } void Reverse(int[] nums, int start, int end) { while (start < end) { Swap(nums, start, end); start++; end--; } }
Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the lowest ascending order.
✗ Incorrect
The array is the highest permutation, so the next permutation resets to the lowest: sorted ascending.
🔧 Debug
advancedIdentify the error in this next permutation code
What error will this code produce when run on input
[1, 2, 3]?DSA C#
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); } void Swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } void Reverse(int[] nums, int start, int end) { while (start < end) { Swap(nums, start, end); start++; end--; } }
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The code uses strict comparison signs (> instead of >= and < instead of <=), but for distinct elements like [1,2,3], it correctly finds the pivot i=1 and produces [1,3,2].
🧠 Conceptual
advancedHow many next permutations exist for an array of length 3?
For an array with 3 distinct elements, how many unique next permutations can be generated by repeatedly applying the next permutation algorithm until it cycles back?
Attempts:
2 left
💡 Hint
Think about how many ways to arrange 3 distinct elements.
✗ Incorrect
There are 3! = 6 permutations total, so 6 unique next permutations cycle through all.
🚀 Application
expertFind the 4th next permutation of [1, 2, 3]
Starting from the array
[1, 2, 3], what is the array after applying the next permutation algorithm 4 times in a row?DSA C#
int[] arr = {1, 2, 3}; for (int k = 0; k < 4; k++) { NextPermutation(arr); } Console.WriteLine(string.Join(", ", arr)); 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); } void Swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } void Reverse(int[] nums, int start, int end) { while (start < end) { Swap(nums, start, end); start++; end--; } }
Attempts:
2 left
💡 Hint
List permutations in order and count four steps from the start.
✗ Incorrect
The permutations in order are: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]. The 4th next permutation after [1,2,3] is [3,1,2].
