0
0
DSA Cprogramming~20 mins

Generate All Combinations Sum K in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combinations summing to K
What is the output of the following C code that generates all combinations of numbers from 1 to 4 that sum to 5?
DSA C
#include <stdio.h>
#include <stdlib.h>

void backtrack(int start, int target, int *comb, int comb_len) {
    if (target == 0) {
        for (int i = 0; i < comb_len; i++) {
            printf("%d", comb[i]);
            if (i < comb_len - 1) printf(",");
        }
        printf("\n");
        return;
    }
    for (int i = start; i <= 4; i++) {
        if (i > target) break;
        comb[comb_len] = i;
        backtrack(i + 1, target - i, comb, comb_len + 1);
    }
}

int main() {
    int comb[10];
    backtrack(1, 5, comb, 0);
    return 0;
}
A
[1,4]
[2,3]
B
[1,4]
[2,3]
[5]
C
[1,4]
[2,3]
[1,2,2]
D
[1,4]
[2,3]
[3,2]
Attempts:
2 left
💡 Hint
Think about combinations of unique numbers from 1 to 4 that add up to 5.
🧠 Conceptual
intermediate
1:30remaining
Understanding combination generation constraints
In generating all combinations of numbers from 1 to N that sum to K, which constraint ensures combinations do not repeat the same number multiple times?
AAllow numbers to be reused by resetting start index to 1 each time
BUse a global visited array to mark used numbers
CSort the input array before generating combinations
DIncrement the start index in recursive calls to avoid reusing numbers
Attempts:
2 left
💡 Hint
Think about how recursion controls which numbers can be chosen next.
🔧 Debug
advanced
2:00remaining
Identify the error in combination sum code
What error does the following C code produce when generating combinations summing to 7 from numbers 1 to 5? void backtrack(int start, int target, int *comb, int comb_len) { if (target == 0) { for (int i = 0; i < comb_len; i++) printf("%d ", comb[i]); printf("\n"); return; } for (int i = start; i <= 5; i++) { if (i >= target) break; comb[comb_len] = i; backtrack(i + 1, target - i, comb, comb_len + 1); } } int main() { int comb[10]; backtrack(1, 7, comb, 0); return 0; }
ACompilation error due to missing return type
BInfinite recursion causing stack overflow
CNo output because the condition 'i >= target' skips valid combinations
DOutputs combinations including numbers greater than 7
Attempts:
2 left
💡 Hint
Check the condition that stops the loop inside backtrack.
🚀 Application
advanced
1:30remaining
Number of combinations summing to K
How many unique combinations of numbers from 1 to 6 sum exactly to 6, assuming each number can be used only once?
A5
B6
C4
D7
Attempts:
2 left
💡 Hint
List all combinations carefully without repeating numbers.
📝 Syntax
expert
1:00remaining
Correct syntax for recursive combination function
Which option correctly declares a recursive function in C to generate all combinations summing to K?
Avoid backtrack(int start, int target, int *comb, int comb_len);
Bvoid backtrack(int start, int target, int comb[], int comb_len) {}
Cvoid backtrack(start int, target int, int *comb, int comb_len) {}
Dint backtrack(int start, int target, int *comb, int comb_len) {}
Attempts:
2 left
💡 Hint
Check parameter types and syntax for array parameters in C.