Distinct Coin Combinations (Order-Independent DP / Recursion)

Difficulty: Hard · Unit 2: Control Flow, Scope & Recursion · Featured in: Tough Challenge

Given K distinct positive coin denominations and a target amount A, find the number of distinct combinations (order does NOT matter: 1+2 and 2+1 count once) that sum to A. Print the count modulo 1,000,000,007.

Input & Output Format

Input: Line 1: two integers K and A. Line 2: K space-separated coin values.

Output: Print a single integer: number of combinations mod 10^9 + 7.

Sample 1

3 5
1 2 5
4

Ways: [5], [2+2+1], [2+1+1+1], [1+1+1+1+1].

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub