Challenge - 5 Problems
Combination Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of combinations summing to K
What is the output of the following Java code that generates all combinations of numbers from 1 to 4 that sum to 5?
DSA Java
import java.util.*; public class Main { static void findCombinations(int start, int k, int target, List<Integer> current, List<List<Integer>> result) { if (target == 0 && current.size() == k) { result.add(new ArrayList<>(current)); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 4; i++) { current.add(i); findCombinations(i + 1, k, target - i, current, result); current.remove(current.size() - 1); } } public static void main(String[] args) { List<List<Integer>> result = new ArrayList<>(); findCombinations(1, 2, 5, new ArrayList<>(), result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about combinations of size 2 that add up to 5 using numbers 1 to 4 without repetition.
✗ Incorrect
The code finds all unique combinations of 2 numbers from 1 to 4 that sum to 5. The valid pairs are [1,4] and [2,3]. The code avoids duplicates by increasing the start index each recursion.
🧠 Conceptual
intermediateNumber of combinations of size K summing to target
Given numbers from 1 to 5, how many unique combinations of size 3 sum exactly to 7?
Attempts:
2 left
💡 Hint
Try listing all combinations of 3 numbers from 1 to 5 and check which sum to 7.
✗ Incorrect
The valid unique combinations without repetition are [1,2,4], [1,3,3] invalid (3 repeats), [2,3,2] invalid (2 repeats), [1,5,1] invalid (1 repeats). So only [1,2,4], [1,3,3] invalid, [2,3,2] invalid. The correct valid combinations are [1,2,4], [1,3,3] invalid, [2,3,2] invalid. So answer is 4.
🔧 Debug
advancedIdentify the error in combination sum code
What error does the following Java code produce when trying to generate all combinations of size 3 summing to 6?
DSA Java
import java.util.*; public class Main { static void findCombinations(int start, int k, int target, List<Integer> current, List<List<Integer>> result) { if (target == 0 && current.size() == k) { result.add(new ArrayList<>(current)); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 5; i++) { current.add(i); findCombinations(i, k, target - i, current, result); current.remove(current.size() - 1); } } public static void main(String[] args) { List<List<Integer>> result = new ArrayList<>(); findCombinations(1, 3, 6, new ArrayList<>(), result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check the recursive call's start parameter and how it affects the loop.
✗ Incorrect
The recursive call uses 'i' as the start parameter instead of 'i + 1', so the same number can be chosen repeatedly causing infinite recursion and eventually StackOverflowError.
📝 Syntax
advancedIdentify the syntax error in combination sum code
Which option contains the syntax error in this Java method to generate combinations?
DSA Java
void findCombinations(int start, int k, int target, List<Integer> current, List<List<Integer>> result) { if (target == 0 && current.size() == k) { result.add(new ArrayList<>(current)); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 5; i++) { current.add(i) findCombinations(i + 1, k, target - i, current, result); current.remove(current.size() - 1); } }
Attempts:
2 left
💡 Hint
Look carefully at the line with current.add(i)
✗ Incorrect
The line current.add(i) is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertFind all unique combinations of size K summing to target with duplicates allowed
Given numbers from 1 to 3, which output correctly shows all unique combinations of size 3 that sum to 5 when numbers can be repeated?
DSA Java
import java.util.*; public class Main { static void findCombinations(int start, int k, int target, List<Integer> current, List<List<Integer>> result) { if (target == 0 && current.size() == k) { result.add(new ArrayList<>(current)); return; } if (target < 0 || current.size() > k) return; for (int i = start; i <= 3; i++) { current.add(i); findCombinations(i, k, target - i, current, result); current.remove(current.size() - 1); } } public static void main(String[] args) { List<List<Integer>> result = new ArrayList<>(); findCombinations(1, 3, 5, new ArrayList<>(), result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Numbers can repeat and order does not matter, so combinations like [1,3,1] and [3,1,1] are considered the same if order is ignored.
✗ Incorrect
The code allows repeated numbers by calling findCombinations with start = i, so numbers can be reused. The output lists unique combinations of size 3 summing to 5 without duplicates. Option D correctly lists unique combinations without duplicates.
