Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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].
✗ Incorrect
We look for the first index i from the right where nums[i] is less than nums[i+1], so we continue while nums[i] >= nums[i+1].
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causes skipping equal values.
Using '>=' causes the loop to stop too late.
✗ Incorrect
We want to find the first index j from the right where nums[j] is greater than nums[i], so we continue while nums[j] <= nums[i].
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with nums[i] again causes no change.
Using wrong indices causes incorrect swap.
✗ Incorrect
To swap nums[i] and nums[j], we assign nums[i] = nums[j], then nums[j] = temp.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes swapping the middle element twice.
Using '--' on start moves it backward incorrectly.
✗ Incorrect
We reverse while start < end, and increment start with start++ each loop.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The blanks correspond to the conditions and loop control: i moves left while nums[i]>=nums[i+1], j moves left while nums[j]<=nums[i], and reversing while start
