Bird
0
0
DSA Cprogramming~20 mins

Next Permutation of Array in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A1 3 2
B3 2 1
C1 2 3
D2 1 3
Attempts:
2 left
💡 Hint
Remember next permutation finds the next lexicographically greater arrangement.
Predict Output
intermediate
2: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;
}
A1 3 2 4
B4 3 2 1
C1 2 3 4
D2 1 3 4
Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the lowest ascending order.
🔧 Debug
advanced
2: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;
}
AThe code runs correctly and outputs: 2 1 3
BThe code swaps wrong elements and outputs: 1 3 2
CThe code causes an infinite loop
DThe code runs correctly and outputs: 1 2 3
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
🧠 Conceptual
advanced
1:30remaining
Understanding the pivot in next permutation
In the next permutation algorithm, what does the pivot index represent?
AThe index of the smallest element in the array
BThe last index of the array
CThe first index from the right where the sequence stops increasing
DThe first index from the right where the sequence stops decreasing
Attempts:
2 left
💡 Hint
Think about where the array stops descending from the right side.
Predict Output
expert
2: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;
}
A1 2 3 3 2
B1 2 3 2 3
C1 3 2 2 3
D1 2 2 3 3
Attempts:
2 left
💡 Hint
The next permutation must be the next lexicographically bigger sequence.