Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the next permutation by swapping the right elements.
DSA Rust
fn next_permutation(nums: &mut Vec<i32>) {
let mut i = nums.len() as i32 - 2;
while i >= 0 && nums[i as usize] >= nums[i as usize + 1] {
i -= [1];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will cause an infinite loop.
Using 2 skips elements and misses the correct pivot.
✗ Incorrect
We decrease i by 1 to move leftwards through the array to find the first decreasing element.
2fill in blank
mediumComplete the code to find the index j to swap with i.
DSA Rust
fn next_permutation(nums: &mut Vec<i32>) {
let mut i = nums.len() as i32 - 2;
while i >= 0 && nums[i as usize] >= nums[i as usize + 1] {
i -= 1;
}
if i >= 0 {
let mut j = nums.len() as i32 - 1;
while nums[j as usize] <= nums[i as usize] {
j -= [1];
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 causes an infinite loop.
Using 2 skips elements and misses the correct j.
✗ Incorrect
We decrease j by 1 to find the rightmost element greater than nums[i].
3fill in blank
hardFix the error in swapping elements at i and j.
DSA Rust
fn next_permutation(nums: &mut Vec<i32>) {
let mut i = nums.len() as i32 - 2;
while i >= 0 && nums[i as usize] >= nums[i as usize + 1] {
i -= 1;
}
if i >= 0 {
let mut j = nums.len() as i32 - 1;
while nums[j as usize] <= nums[i as usize] {
j -= 1;
}
nums.swap(i as usize, [1]);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping i with itself does nothing.
Swapping with 0 or last index is incorrect here.
✗ Incorrect
We swap the elements at i and j to get the next permutation step.
4fill in blank
hardFill both blanks to reverse the suffix after index i.
DSA Rust
fn next_permutation(nums: &mut Vec<i32>) {
let mut i = nums.len() as i32 - 2;
while i >= 0 && nums[i as usize] >= nums[i as usize + 1] {
i -= 1;
}
if i >= 0 {
let mut j = nums.len() as i32 - 1;
while nums[j as usize] <= nums[i as usize] {
j -= 1;
}
nums.swap(i as usize, j as usize);
}
let mut left = (i + 1) as usize;
let mut right = nums.len() - 1;
while left < right {
nums.swap([1], [2]);
left += 1;
right -= 1;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using i or j here is incorrect because we reverse the suffix after i.
Swapping wrong indices breaks the order.
✗ Incorrect
We swap elements at left and right to reverse the suffix after i.
5fill in blank
hardFill all three blanks to complete the next_permutation function.
DSA Rust
fn next_permutation(nums: &mut Vec<i32>) {
let mut i = nums.len() as i32 - 2;
while i >= 0 && nums[i as usize] >= nums[i as usize + 1] {
i -= [1];
}
if i >= 0 {
let mut j = nums.len() as i32 - 1;
while nums[j as usize] <= nums[i as usize] {
j -= [2];
}
nums.swap(i as usize, j as usize);
}
let mut left = (i + 1) as usize;
let mut right = nums.len() - 1;
while left < right {
nums.swap([3], right);
left += 1;
right -= 1;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or wrong indices causes infinite loops or wrong swaps.
Not reversing the suffix breaks the next permutation logic.
✗ Incorrect
Decrease i and j by 1 each time, and swap elements at left and right to reverse suffix.
