0
0
DSA Cprogramming~10 mins

Generate All Combinations Sum K 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 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'
A1
B0
Ck
D-1
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.
2fill in blank
medium

Complete 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'
A>
B!=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < causes incorrect matches.
Using != prints wrong combinations.
3fill in blank
hard

Fix 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'
Ai + 1
Bi
Cstart
DcombSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using start causes infinite recursion.
Using i or combSize causes wrong indices.
4fill in blank
hard

Fill 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'
A>
B==
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == to stop causes missing some cases.
Using <= to continue causes infinite loops.
5fill in blank
hard

Fill 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'
A<
B==
C!=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == in loop condition causes only one iteration.
Using == for comma check prints comma after last element.