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 is the array after applying the next permutation algorithm once?
DSA Go
arr := []int{1, 3, 2} // Apply next permutation algorithm here // Print arr after modification fmt.Println(arr)
Attempts:
2 left
💡 Hint
Look for the first pair from the right where the left number is smaller than the right number.
✗ Incorrect
The next permutation of [1, 3, 2] is [2, 1, 3]. So option B is correct.
❓ Predict Output
intermediateWhat is the output after next permutation on sorted descending array?
Given the array [3, 2, 1], what is the array after applying the next permutation algorithm once?
DSA Go
arr := []int{3, 2, 1} // Apply next permutation algorithm here // Print arr after modification fmt.Println(arr)
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 permutation [1, 2, 3].
🔧 Debug
advancedIdentify the error in this next permutation code snippet
What error will this Go code produce when trying to find the next permutation of [3, 2, 1]?
func nextPermutation(nums []int) {
i := len(nums) - 2
for i >= 0 && nums[i] >= nums[i+1] {
i--
}
j := len(nums) - 1
for i >= 0 && nums[j] <= nums[i] {
j--
}
if i >= 0 {
nums[i], nums[j] = nums[j], nums[i]
}
reverse(nums[i+1:])
}
func reverse(nums []int) {
for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
}
DSA Go
func nextPermutation(nums []int) { i := len(nums) - 2 for i >= 0 && nums[i] >= nums[i+1] { i-- } j := len(nums) - 1 for i >= 0 && nums[j] <= nums[i] { j-- } if i >= 0 { nums[i], nums[j] = nums[j], nums[i] } reverse(nums[i+1:]) } func reverse(nums []int) { for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 { nums[i], nums[j] = nums[j], nums[i] } }
Attempts:
2 left
💡 Hint
Check what happens when i becomes -1 and is used as an index.
✗ Incorrect
If the array is already the highest permutation, i becomes -1. Then nums[i] causes an index out of range panic.
🧠 Conceptual
advancedWhat is the time complexity of the next permutation algorithm?
Consider the standard next permutation algorithm that finds the next lexicographical permutation of an array of length n. What is its time complexity?
Attempts:
2 left
💡 Hint
Think about how many times the array is scanned and reversed.
✗ Incorrect
The algorithm scans the array from the end once to find the pivot, then scans again to find the swap element, and finally reverses a suffix. All these steps are linear in n.
🚀 Application
expertHow many unique permutations can be generated from [1, 2, 2] using next permutation?
Starting from the sorted array [1, 2, 2], how many unique permutations will the next permutation algorithm generate before returning to the original array?
Attempts:
2 left
💡 Hint
Count all unique permutations of [1, 2, 2].
✗ Incorrect
The unique permutations of [1, 2, 2] are: [1, 2, 2], [2, 1, 2], and [2, 2, 1]. So there are 3 unique permutations.
