Challenge - 5 Problems
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Next Permutation on a simple array
What is the output of the following C code after calling next_permutation on the array?
DSA C
void next_permutation(int* nums, int numsSize) { int i = numsSize - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } if (i >= 0) { int j = numsSize - 1; while (nums[j] <= nums[i]) { j--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } int left = i + 1, right = numsSize - 1; while (left < right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } #include <stdio.h> int main() { int arr[3] = {1, 2, 3}; next_permutation(arr, 3); for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Remember next permutation finds the next lexicographically greater arrangement.
✗ Incorrect
The next permutation of [1, 2, 3] is [1, 3, 2]. The code swaps the first decreasing element from the right with the next bigger element and reverses the suffix.
❓ Predict Output
intermediate2:00remaining
Next Permutation output for descending array
What will be printed after running next_permutation on this descending array?
DSA C
void next_permutation(int* nums, int numsSize) { int i = numsSize - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } if (i >= 0) { int j = numsSize - 1; while (nums[j] <= nums[i]) { j--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } int left = i + 1, right = numsSize - 1; while (left < right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } #include <stdio.h> int main() { int arr[4] = {4, 3, 2, 1}; next_permutation(arr, 4); for (int i = 0; i < 4; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the lowest ascending order.
✗ Incorrect
The array is the highest permutation. The next permutation resets it to the lowest: [1, 2, 3, 4].
🔧 Debug
advanced2:00remaining
Identify the error in this next_permutation implementation
What error will this code cause when run on array [1, 3, 2]?
DSA C
void next_permutation(int* nums, int numsSize) { int i = numsSize - 2; while (i >= 0 && nums[i] > nums[i + 1]) { i--; } if (i >= 0) { int j = numsSize - 1; while (nums[j] < nums[i]) { j--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } int left = i + 1, right = numsSize - 1; while (left < right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } #include <stdio.h> int main() { int arr[3] = {1, 3, 2}; next_permutation(arr, 3); for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
✗ Incorrect
The code uses '>' and '<' instead of '>=' and '<=' causing incorrect pivot and swap selection, so the output remains unchanged.
🧠 Conceptual
advanced1:30remaining
Understanding the pivot in next permutation
In the next permutation algorithm, what does the pivot index represent?
Attempts:
2 left
💡 Hint
Think about where the array stops descending from the right side.
✗ Incorrect
The pivot is the first index from the right where the sequence stops decreasing (or stops non-increasing). This is where we find the element to swap to get the next bigger permutation.
❓ Predict Output
expert2:30remaining
Next Permutation output for array with duplicates
What is the output after running next_permutation on this array with duplicates?
DSA C
void next_permutation(int* nums, int numsSize) { int i = numsSize - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } if (i >= 0) { int j = numsSize - 1; while (nums[j] <= nums[i]) { j--; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } int left = i + 1, right = numsSize - 1; while (left < right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } #include <stdio.h> int main() { int arr[5] = {1, 2, 2, 3, 3}; next_permutation(arr, 5); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
The next permutation must be the next lexicographically bigger sequence.
✗ Incorrect
The next permutation after [1, 2, 2, 3, 3] is [1, 2, 3, 3, 2]. The pivot is at index 2 (value 2), swapped with index 4 (value 3), then suffix reversed.
