- 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

# Program to find number of distinct combinations that sum up to k in python

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:

## Example Code

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))

## Input

[2, 4, 5], 4

## Output

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 max number of K-sum pairs in Python
- Program to find number of distinct subsequences in Python
- Program to find any two numbers in a list that sums up to k in Python
- Program to find number of combinations of coins to reach target 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 number of sublists with sum k in a binary list in Python
- Program to find the sum of largest K sublist in Python
- Program to find sum of digits in base K using Python
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Python program to find N-sized substrings with K distinct characters

Advertisements