Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the next permutation by swapping the rightmost pair where the left element is smaller.
DSA Typescript
for (let i = nums.length - 2; i >= 0; i--) { if (nums[i] [1] nums[i + 1]) { // Found the first decreasing element break; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes the loop to find the wrong pivot.
Using '==' or '!=' does not help find the pivot.
✗ Incorrect
We look for the first index from the right where nums[i] is less than nums[i+1] to find the pivot for the next permutation.
2fill in blank
mediumComplete the code to find the element to swap with the pivot found at index i.
DSA Typescript
for (let j = nums.length - 1; j > i; j--) { if (nums[j] [1] nums[i]) { // Swap nums[i] and nums[j] [nums[i], nums[j]] = [nums[j], nums[i]]; break; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes swapping with a smaller element.
Using '==' or '!=' does not find the correct swap candidate.
✗ Incorrect
We find the first element from the right that is greater than nums[i] to swap and get the next permutation.
3fill in blank
hardFix the error in reversing the suffix of the array after swapping.
DSA Typescript
let start = i + 1; let end = nums.length - 1; while (start [1] end) { [nums[start], nums[end]] = [nums[end], nums[start]]; start++; end--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes swapping the middle element twice.
Using '>=' or '>' causes the loop to never run or run incorrectly.
✗ Incorrect
We reverse the suffix by swapping elements while start is less than end, not less or equal.
4fill in blank
hardFill both blanks to complete the function that finds the next permutation of the array.
DSA Typescript
function nextPermutation(nums: number[]): void {
let i = nums.length - 2;
while (i >= 0 && nums[i] [1] nums[i + 1]) {
i--;
}
if (i >= 0) {
let j = nums.length - 1;
while (nums[j] [2] nums[i]) {
j--;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
}
let start = i + 1;
let end = nums.length - 1;
while (start < end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' in these loops causes incorrect pivot or swap index.
Mixing up the comparison operators leads to wrong next permutation.
✗ Incorrect
The first while loop finds the first index from right where nums[i] > nums[i+1]. The second while loop finds the first index j from right where nums[j] > nums[i].
5fill in blank
hardFill all three blanks to implement the next permutation function correctly.
DSA Typescript
function nextPermutation(nums: number[]): void {
let i = nums.length - 2;
while (i >= 0 && nums[i] [1] nums[i + 1]) {
i--;
}
if (i >= 0) {
let j = nums.length - 1;
while (nums[j] [2] nums[i]) {
j--;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
}
let start = i + 1;
let end = nums.length - 1;
while (start [3] end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in the last loop causes double swapping of the middle element.
Using '<' or '<=' incorrectly in the first two loops causes wrong pivot or swap index.
✗ Incorrect
The first two loops use '>' to find the pivot and swap index. The last loop uses '<' to reverse the suffix correctly.
