377. Combination Sum IV

Combination Sum IV (LeetCode 377) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern.

How to solve Combination Sum IV

Combination Sum IV is a Coin Change problem, part of Dynamic Programming (DP) Patterns. Solve coin change and unbounded knapsack problems where items can be reused.

Work towards the solution in these steps:

  1. Think about using dynamic programming to build up the number of combinations for each value from 0 to target.
  2. Consider creating a 1D DP array where each index represents the number of combinations to reach that sum.
  3. For each target sum, iterate through the nums array and check if you can use the current number to contribute to that sum.

The reference solution runs in O(nlogn + n * t) time and O(t) space.

The Combination Sum IV solution and editorial walks through the full approach with reference code.

Problem statement

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 <= 200

- 1 <= nums[i] <= 1000

- All the elements of nums are unique.

- 1 <= target <= 1000

Follow 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?

Topics and companies

Topics: Array, Dynamic Programming.

Reported in interviews at Bloomberg, Wish, Apple.

How to practise Combination Sum IV on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(nlogn + n * t) time and O(t) space.

Open Combination Sum IV in the code editor, or read the Combination Sum IV editorial for a worked solution with its approach and complexity analysis.

Where Combination Sum IV sits in the DSA pattern sheet

Problems related to Combination Sum IV

Other problems that use the same 1D Array (Coin Change / Unbounded Knapsack Style) and Dynamic Programming (DP) Patterns techniques:

Combination Sum IV FAQ

What is the LeetCode number of Combination Sum IV?

Combination Sum IV is LeetCode problem 377, rated Medium.

What pattern is used to solve Combination Sum IV?

Combination Sum IV is a Coin Change problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern.

What is the time complexity of Combination Sum IV?

The reference solution runs in O(nlogn + n * t) time and O(t) space.

Which companies ask Combination Sum IV?

Combination Sum IV has been reported in interviews at Bloomberg, Wish, Apple.

Preparing this workspace.