Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the recursive function call with the starting index 0.
DSA C
void findCombinations(int* arr, int n, int k, int start, int* combination, int combSize) {
// recursive code here
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int k = 5;
int combination[10];
findCombinations(arr, 5, k, [1], combination, 0);
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from index 1 skips the first element.
Using k or -1 as start index causes out-of-bound errors.
✗ Incorrect
The recursive function should start checking combinations from index 0 of the array.
2fill in blank
mediumComplete the code to check if the current sum equals the target sum k.
DSA C
void findCombinations(int* arr, int n, int k, int start, int* combination, int combSize) {
int sum = 0;
for (int i = 0; i < combSize; i++) {
sum += combination[i];
}
if (sum [1] k) {
// print combination
return;
}
// continue recursion
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < causes incorrect matches.
Using != prints wrong combinations.
✗ Incorrect
We want to check if the sum of the current combination exactly equals k.
3fill in blank
hardFix the error in the recursive call to avoid infinite recursion by moving the start index forward.
DSA C
for (int i = start; i < n; i++) { combination[combSize] = arr[i]; findCombinations(arr, n, k, [1], combination, combSize + 1); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start causes infinite recursion.
Using i or combSize causes wrong indices.
✗ Incorrect
To avoid repeating the same element, the next recursive call should start from the next index i + 1.
4fill in blank
hardFill both blanks to stop recursion when sum exceeds k and to continue otherwise.
DSA C
int sum = 0; for (int i = 0; i < combSize; i++) { sum += combination[i]; } if (sum [1] k) { return; } else if (sum [2] k) { // continue recursion }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == to stop causes missing some cases.
Using <= to continue causes infinite loops.
✗ Incorrect
If sum is greater than k, stop recursion. If sum is less than k, continue searching.
5fill in blank
hardFill all three blanks to print the current combination correctly.
DSA C
if (sum == k) { printf("Combination: "); for (int i = 0; i [1] combSize; i++) { printf("%d", combination[i]); if (i [2] combSize - 1) { printf(", "); } } printf("\n"); return; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == in loop condition causes only one iteration.
Using == for comma check prints comma after last element.
✗ Incorrect
Loop from i < combSize; print comma if i != combSize - 1; use <= for safe boundary checks.