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 function?
Given the array [1, 2, 3], what will be the state of the array after applying the next permutation algorithm once?
DSA Javascript
function nextPermutation(nums) {
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}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 of [1, 2, 3] is [1, 3, 2] because we find the first element from the right that is smaller than its next element (2), then swap it with the next bigger element (3), and finally reverse the suffix.
❓ Predict Output
intermediateWhat is the output after next permutation on [3, 2, 1]?
Apply the next permutation algorithm on the array [3, 2, 1]. What is the resulting array?
DSA Javascript
function nextPermutation(nums) {
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the ascending order.
✗ Incorrect
The array [3, 2, 1] is the highest permutation. The next permutation resets it to the lowest, which is [1, 2, 3].
🔧 Debug
advancedWhy does this next permutation code fail on [1, 5, 1]?
The following code is intended to compute the next permutation but produces incorrect output for [1, 5, 1]. What is the output and why is it wrong?
Code:
function nextPermutation(nums) {
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops for pivot and successor selection.
✗ Incorrect
The code uses > and < instead of >= and <=, causing it to pick the wrong pivot and successor, resulting in the output [1, 1, 5] instead of the correct next permutation [5, 1, 1].
🧠 Conceptual
advancedWhat is the time complexity of the next permutation algorithm?
Consider the standard next permutation algorithm that finds the next lexicographically greater permutation of an array. What is its time complexity in terms of the array length n?
Attempts:
2 left
💡 Hint
Think about how many times the array is scanned or reversed.
✗ Incorrect
The algorithm scans the array from the end to find the pivot and successor, then reverses the suffix. Each step is linear, so overall O(n).
🚀 Application
expertHow many unique permutations are generated by repeatedly applying next permutation on [1, 1, 2]?
Starting from the array [1, 1, 2], if you repeatedly apply the next permutation algorithm until it cycles back to the original array, how many unique permutations will be generated?
Attempts:
2 left
💡 Hint
Count distinct permutations considering duplicates in the array.
✗ Incorrect
The array [1, 1, 2] has 3 unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Repeated next permutation cycles through these 3.
