| 8 | 9 | * @returns {number[][]} |
| 9 | 10 | */ |
| 10 | 11 | export const combinations = (cage) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Invalid input parameters |
| 13 | if (!cage || typeof cage.sum !== 'number' || typeof cage.size !== 'number' || !Array.isArray(cage.exclude)) { |
| 14 | return []; |
| 15 | } |
| 16 | |
| 17 | const { sum, size, exclude } = cage; |
| 18 | |
| 19 | // Edge Case: Invalid size |
| 20 | if (size <= 0 || size > 9) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Invalid sum |
| 25 | if (sum <= 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | // Edge Case: Sum too large for the given size |
| 30 | const maxPossibleSum = Array(size).fill(0).map((_, i) => 9 - i).reduce((a, b) => a + b, 0); |
| 31 | if (sum > maxPossibleSum) { |
| 32 | return []; |
| 33 | } |
| 34 | |
| 35 | // Edge Case: Sum too small for the given size |
| 36 | const minPossibleSum = Array(size).fill(0).map((_, i) => i + 1).reduce((a, b) => a + b, 0); |
| 37 | if (sum < minPossibleSum) { |
| 38 | return []; |
| 39 | } |
| 40 | |
| 41 | // Create a set of available digits (1-9 excluding the excluded ones) |
| 42 | const excludeSet = new Set(exclude); |
| 43 | const availableDigits = []; |
| 44 | for (let i = 1; i <= 9; i++) { |
| 45 | if (!excludeSet.has(i)) { |
| 46 | availableDigits.push(i); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Edge Case: Not enough available digits for the cage size |
| 51 | if (availableDigits.length < size) { |
| 52 | return []; |
| 53 | } |
| 54 | |
| 55 | const result = []; |
| 56 | |
| 57 | // Recursive function to find all combinations |
| 58 | function findCombinations(startIndex, currentCombination, remainingSum, remainingSize) { |
| 59 | // Edge Case: Found a valid combination |
| 60 | if (remainingSize === 0 && remainingSum === 0) { |
| 61 | result.push([...currentCombination]); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // Edge Case: Impossible to reach the target |
| 66 | if (remainingSize === 0 || remainingSum <= 0) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Not enough digits left |
| 71 | if (startIndex >= availableDigits.length) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Try each available digit from startIndex onwards |
| 76 | for (let i = startIndex; i < availableDigits.length; i++) { |
| 77 | const digit = availableDigits[i]; |
| 78 | |
| 79 | // Edge Case: Digit is too large for remaining sum |
| 80 | if (digit > remainingSum) { |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | currentCombination.push(digit); |
| 85 | findCombinations(i + 1, currentCombination, remainingSum - digit, remainingSize - 1); |
| 86 | currentCombination.pop(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | findCombinations(0, [], sum, size); |
| 91 | |
| 92 | // Sort the combinations |
| 93 | result.sort((a, b) => { |
| 94 | for (let i = 0; i < Math.min(a.length, b.length); i++) { |
| 95 | if (a[i] !== b[i]) { |
| 96 | return a[i] - b[i]; |
| 97 | } |
| 98 | } |
| 99 | return a.length - b.length; |
| 100 | }); |
| 101 | |
| 102 | return result; |
| 12 | 103 | }; |
| 104 | |
| 105 | } |