#K72402. Coin Change Problem
Coin Change Problem
Coin Change Problem
You are given a set of coin denominations and a target amount \( m \). Your task is to find the minimum number of coins required to make the amount \( m \) using any combination of the given coins. If it is not possible to form the amount, output \( -1 \).
Use the following recurrence in your solution:
\( dp[i] = \min(dp[i], dp[i-coin] + 1) \) for all coin denominations, where \( dp[i] \) represents the minimum number of coins required for amount \( i \).
Example:
- For coins = [1, 2, 5] and m = 11, the answer is 3 because \( 11 = 5 + 5 + 1 \).
- For coins = [2, 4] and m = 3, it is not possible to form 3 and the answer is \( -1 \).
inputFormat
The first line contains an integer \( T \), the number of test cases. Each test case consists of two lines:
- The first line contains two integers \( n \) and \( m \) where \( n \) is the number of coin types and \( m \) is the target amount.
- The second line contains \( n \) space-separated integers representing the coin denominations.
outputFormat
For each test case, output a single line containing the minimum number of coins needed to form the amount \( m \). If it is not possible, output \( -1 \).
## sample1
3 11
1 2 5
3
</p>