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 after applying next permutation?
Given the array [1, 3, 2], what will be the array after applying the next permutation algorithm once?
DSA Typescript
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
const arr = [1, 3, 2];
nextPermutation(arr);
console.log(arr);Attempts:
2 left
💡 Hint
Look for the first decreasing element from the right, then swap and reverse the suffix.
✗ Incorrect
The next permutation finds the next lexicographically greater permutation. For [1,3,2], the next is [2,1,3].
❓ Predict Output
intermediateWhat is the output after next permutation on descending array?
Given the array [3, 2, 1], what will be the array after applying the next permutation algorithm once?
DSA Typescript
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
const arr = [3, 2, 1];
nextPermutation(arr);
console.log(arr);Attempts:
2 left
💡 Hint
If no next permutation exists, the array resets to the lowest order.
✗ Incorrect
The array is in descending order, so the next permutation is the lowest ascending order [1,2,3].
🔧 Debug
advancedIdentify the error in this next permutation implementation
What error will this code produce when run with input [1, 2, 2]?
DSA Typescript
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
const arr = [1, 2, 2];
nextPermutation(arr);
console.log(arr);Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The code uses > and < instead of >= and <=, so it fails to skip equal elements (plateau), picks wrong pivot i=1, finds no strictly larger element to swap, resulting in no change [1,2,2]. Correct next is [2,1,2].
🧠 Conceptual
advancedHow many permutations are possible for array [1, 2, 3]?
Considering all unique permutations of the array [1, 2, 3], how many distinct permutations exist?
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial (n!).
✗ Incorrect
For 3 distinct elements, total permutations = 3! = 6.
❓ Predict Output
expertWhat is the 4th next permutation of [1, 2, 3]?
Starting from the array [1, 2, 3], if you apply the next permutation algorithm 4 times consecutively, what will be the resulting array?
DSA Typescript
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]];
}
let left = i + 1, right = nums.length - 1;
while (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
const arr = [1, 2, 3];
for(let k = 0; k < 4; k++) {
nextPermutation(arr);
}
console.log(arr);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].
