Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
The test cases are generated so that the answer can fit in a 32-bit integer.
Example 1:
Input: nums = [1,2,3], target = 4 Output: 7 Explanation: The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3 Output: 0
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 1000nums are unique.1 <= target <= 1000Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
Problem summary: Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target. The test cases are generated so that the answer can fit in a 32-bit integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3] 4
[9] 3
combination-sum)ways-to-express-an-integer-as-sum-of-powers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #377: Combination Sum IV
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] f = new int[target + 1];
f[0] = 1;
for (int i = 1; i <= target; ++i) {
for (int x : nums) {
if (i >= x) {
f[i] += f[i - x];
}
}
}
return f[target];
}
}
// Accepted solution for LeetCode #377: Combination Sum IV
func combinationSum4(nums []int, target int) int {
f := make([]int, target+1)
f[0] = 1
for i := 1; i <= target; i++ {
for _, x := range nums {
if i >= x {
f[i] += f[i-x]
}
}
}
return f[target]
}
# Accepted solution for LeetCode #377: Combination Sum IV
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
f = [1] + [0] * target
for i in range(1, target + 1):
for x in nums:
if i >= x:
f[i] += f[i - x]
return f[target]
// Accepted solution for LeetCode #377: Combination Sum IV
struct Solution;
impl Solution {
fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
let k = target as usize;
let mut dp = vec![0; k + 1];
dp[0] = 1;
for i in 1..=target {
for &j in &nums {
if i - j >= 0 {
dp[i as usize] += dp[(i - j) as usize];
}
}
}
dp[k]
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3];
let target = 4;
let res = 7;
assert_eq!(Solution::combination_sum4(nums, target), res);
}
// Accepted solution for LeetCode #377: Combination Sum IV
function combinationSum4(nums: number[], target: number): number {
const f: number[] = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
if (i >= x) {
f[i] += f[i - x];
}
}
}
return f[target];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.