Bird
0
0
DSA Cprogramming~10 mins

Next Permutation of Array in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the first index from the end where the array value is less than the next value.

DSA C
int i = n - 2;
while (i >= 0 && nums[i] [1]= nums[i + 1]) {
    i--;
}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes the loop to stop too early.
Using '>' misses the case when nums[i] equals nums[i+1].
2fill in blank
medium

Complete the code to find the first index from the end where the array value is greater than nums[i].

DSA C
int j = n - 1;
while (j >= 0 && nums[j] [1]= nums[i]) {
    j--;
}
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causes skipping equal values.
Using '>=' causes the loop to stop too late.
3fill in blank
hard

Fix the error in swapping nums[i] and nums[j].

DSA C
int temp = nums[i];
nums[i] = nums[[1]];
nums[j] = temp;
Drag options to blanks, or click blank then click option'
Ai + 1
Bj
Ci
Dj - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with nums[i] again causes no change.
Using wrong indices causes incorrect swap.
4fill in blank
hard

Fill both blanks to reverse the subarray from i+1 to end.

DSA C
int start = i + 1;
int end = n - 1;
while (start [1] end) {
    int temp = nums[start];
    nums[start] = nums[end];
    nums[end] = temp;
    start[2];
    end--;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes swapping the middle element twice.
Using '--' on start moves it backward incorrectly.
5fill in blank
hard

Fill all three blanks to complete the next permutation function.

DSA C
void nextPermutation(int* nums, int n) {
    int i = n - 2;
    while (i >= 0 && nums[i] [1]= nums[i + 1]) {
        i--;
    }
    if (i >= 0) {
        int j = n - 1;
        while (j >= 0 && nums[j] [2]= nums[i]) {
            j--;
        }
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    int start = i + 1;
    int end = n - 1;
    while (start [3] end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up comparison operators causes incorrect next permutation.
Using wrong loop conditions leads to infinite loops or wrong output.