- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of distinct numbers called nums and another number k, we have to find the number of distinct combinations that sum up to k. You can reuse numbers when creating combinations.

So, if the input is like nums = [2, 4, 5] k = 4, then the output will be 2, as we can make two such groups like [2, 2] and [4].

To solve this, we will follow these steps:

- table := a list with size k + 1, and fill with 0
- table[0] := 1
- for each num in nums, do
- for i in range num to k, do
- table[i] := table[i] + table[i - num]

- for i in range num to k, do
- return table[k]

Let us see the following implementation to get better understanding:

class Solution: def solve(self, nums, k): table = [1] + [0] * k for num in nums: for i in range(num, k + 1): table[i] += table[i - num] return table[k] ob = Solution() nums = [2, 4, 5] k = 4 print(ob.solve(nums, k))

[2, 4, 5], 4

2

- Related Questions & Answers
- Program to count subsets that sum up to k in python
- Program to find number of distinct quadruple that forms target sum in python
- Program to find number of combinations of coins to reach target in Python
- Program to find any two numbers in a list that sums up to k in Python
- Program to find number of sublists that contains exactly k different words in Python
- Program to Find K-Largest Sum Pairs in Python
- Program to find sum of rectangle whose sum at most k in Python
- Program to find the sum of largest K sublist in Python
- Program to find sum of digits in base K using Python
- Python program to find N-sized substrings with K distinct characters
- Program to count number of paths whose sum is k in python
- Program to find number of subsequences that satisfy the given sum condition using Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find number of increasing subsequences of size k in Python
- How to find the unique combination k sum that corresponds to k sum using C#?

Advertisements