Bird
Raised Fist0
DSA Cprogramming~10 mins

Generate All Combinations Sum K in DSA C - Interactive Practice

Choose your learning style10 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.

Practice

(1/5)
1. What is the main goal of the Generate All Combinations Sum K problem?
easy
A. Count the total number of elements in an array
B. Sort an array of numbers in ascending order
C. Find the maximum number in an array
D. Find all unique combinations of numbers that add up to a target sum K

Solution

  1. Step 1: Understand the problem statement

    The problem asks to find all unique sets of numbers that add up exactly to a given sum K.
  2. Step 2: Identify the goal

    The goal is to generate all possible combinations, not just one or count elements.
  3. Final Answer:

    Find all unique combinations of numbers that add up to a target sum K -> Option D
  4. Quick Check:

    Combination sum = find all sets adding to K [OK]
Hint: Focus on combinations that sum exactly to K [OK]
Common Mistakes:
  • Confusing combination sum with sorting
  • Looking for maximum instead of combinations
  • Counting elements instead of sums
2. Which of the following is the correct way to start a recursive function header in C for generating combinations that sum to K from an array arr of size n?
easy
A. void generate(int arr[], int n, int target, int start, int currentSum, int combination[], int combSize)
B. int generate(int arr, int n, int target, int start, int currentSum, int combination[], int combSize)
C. void generate(int arr, int n, int target, int start, int currentSum, int combination[], int combSize)
D. int generate(int arr[], int n, int target, int start, int currentSum, int combination[], int combSize)

Solution

  1. Step 1: Check parameter types for array and function return

    In C, arrays are passed as int arr[] and recursive functions for generating combinations usually return void.
  2. Step 2: Verify function signature correctness

    void generate(int arr[], int n, int target, int start, int currentSum, int combination[], int combSize) correctly uses void return type and int arr[] for the array parameter.
  3. Final Answer:

    void generate(int arr[], int n, int target, int start, int currentSum, int combination[], int combSize) -> Option A
  4. Quick Check:

    Correct C syntax for recursive combination function [OK]
Hint: Use void return and array as int arr[] in C [OK]
Common Mistakes:
  • Using int instead of int[] for array parameter
  • Returning int instead of void for recursive function
  • Missing array brackets in parameter
3. What will be the output combinations for the array [2, 3, 6, 7] and target sum 7 using combination sum logic?
medium
A. [[7], [2, 2, 3]]
B. [[7], [3, 4]]
C. [[2, 3, 6], [7]]
D. [[2, 2, 3], [6, 1]]

Solution

  1. Step 1: Identify valid combinations summing to 7

    From the array, 7 alone is valid. Also, 2+2+3 = 7 is valid. 3+4 is invalid as 4 is not in array. 6+1 invalid as 1 not in array.
  2. Step 2: Confirm combinations

    Valid combinations are [7] and [2, 2, 3].
  3. Final Answer:

    [[7], [2, 2, 3]] -> Option A
  4. Quick Check:

    Sum 7 from array = [7], [2,2,3] [OK]
Hint: Check sums only from given array elements [OK]
Common Mistakes:
  • Including numbers not in the array
  • Ignoring repeated elements allowed
  • Mixing invalid sums
4. Given the following snippet, what is the bug that prevents correct generation of combinations summing to K?
void generate(int arr[], int n, int target, int start, int currentSum, int combination[], int combSize) {
    if (currentSum == target) {
        // print combination
        return;
    }
    for (int i = start; i < n; i++) {
        if (currentSum + arr[i] <= target) {
            combination[combSize] = arr[i];
            generate(arr, n, target, i, currentSum + arr[i], combination, combSize + 1);
        }
    }
}
medium
A. Incorrect base case condition for currentSum == target
B. Loop should start from 0 instead of start
C. Missing backtracking step to remove last element after recursion
D. Function should return int instead of void

Solution

  1. Step 1: Analyze recursion and combination building

    The function adds elements to combination but never removes them after recursion, causing incorrect combinations.
  2. Step 2: Identify missing backtracking

    Backtracking requires removing last added element after recursive call to explore other paths.
  3. Final Answer:

    Missing backtracking step to remove last element after recursion -> Option C
  4. Quick Check:

    Backtracking needs element removal after recursion [OK]
Hint: Always remove last element after recursive call [OK]
Common Mistakes:
  • Not removing last element after recursion
  • Wrong base case check
  • Loop starting index error
5. You want to generate all unique combinations from array [1, 2, 3] that sum to 4. Which of the following modifications ensures no duplicate combinations appear when numbers can be reused multiple times?
hard
A. Sort the array and skip duplicates by checking if current element equals previous
B. In recursive calls, pass i as start index to allow reuse and avoid duplicates
C. In recursive calls, always pass 0 as start index to restart from beginning
D. Use a global visited array to mark elements used

Solution

  1. Step 1: Understand reuse and duplicates in combinations

    Allowing reuse means next recursion starts from current index i, not 0, to avoid duplicate sets in different orders.
  2. Step 2: Identify correct start index usage

    Passing i as start index allows reuse and prevents duplicates by not revisiting earlier elements.
  3. Final Answer:

    In recursive calls, pass i as start index to allow reuse and avoid duplicates -> Option B
  4. Quick Check:

    Start index = i to reuse and avoid duplicates [OK]
Hint: Pass current index as start to reuse elements [OK]
Common Mistakes:
  • Restarting from 0 causing duplicates
  • Using visited array unnecessarily
  • Not sorting or skipping duplicates properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard