Challenge - 5 Problems
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Next Permutation on a Simple Array
What is the output of the following Java code after calling nextPermutation on the array?
DSA Java
int[] nums = {1, 2, 3}; nextPermutation(nums); System.out.print(Arrays.toString(nums)); 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--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } reverse(nums, i + 1); } private void reverse(int[] nums, int start) { int i = start, j = nums.length - 1; while (i < j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; i++; j--; } }
Attempts:
2 left
💡 Hint
Look for the first decreasing element from the right and swap it with the next bigger element.
✗ Incorrect
The next permutation algorithm finds the first element from the right that is smaller than its next element, swaps it with the next bigger element on the right, then reverses the suffix. For [1,2,3], the next permutation is [1,3,2].
❓ Predict Output
intermediateNext Permutation Output for Descending Array
What will be the output after applying nextPermutation on this array?
DSA Java
int[] nums = {3, 2, 1}; nextPermutation(nums); System.out.print(Arrays.toString(nums)); 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--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } reverse(nums, i + 1); } private void reverse(int[] nums, int start) { int i = start, j = nums.length - 1; while (i < j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; i++; j--; } }
Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the smallest permutation.
✗ Incorrect
When the array is sorted in descending order, the next permutation is the array sorted in ascending order, which is the smallest permutation.
🔧 Debug
advancedIdentify the Bug in Next Permutation Implementation
What error will this code produce when running nextPermutation on the array [1, 2, 2]?
DSA Java
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--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } reverse(nums, i + 1); } private void reverse(int[] nums, int start) { int i = start, j = nums.length - 1; while (i < j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; i++; j--; } }
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The code uses '>=' and '<=' which causes it to miss the correct pivot and swap, resulting in no change to the array.
🧠 Conceptual
advancedUnderstanding the Purpose of Reversing the Suffix
Why does the next permutation algorithm reverse the suffix of the array after swapping the pivot?
Attempts:
2 left
💡 Hint
Think about the order of elements after the pivot swap.
✗ Incorrect
After swapping the pivot with the next bigger element, the suffix is still in descending order. Reversing it sorts it ascendingly, giving the next smallest permutation.
🚀 Application
expertNumber of Next Permutations to Return to Original Array
Given the array [1, 2, 3], how many times must you apply nextPermutation to return to the original array?
Attempts:
2 left
💡 Hint
Consider how many unique permutations exist for 3 distinct elements.
✗ Incorrect
There are 3! = 6 permutations of [1, 2, 3]. Applying nextPermutation 6 times cycles through all permutations and returns to the original array.
