ou are given an array of integers ‘coins’ denoting the denomination of coins and another array of integers ‘freq’ denoting the number of coins of each denomination.You have to find the number of ways to make the sum ‘V’ by selecting some(or all) coins from the array.The answer can be very large. So, return the answer modulo 1000000007.For Example :‘N’ = 3, ‘coins’ = {1, 2, 3}, ‘freq’ = {1, 1, 3}, ‘V’ = 6For the given example, we can make six by using the following coins:{1, 2, 3}{3. 3}Hence, the answer is 2.
Question
ou are given an array of integers ‘coins’ denoting the denomination of coins and another array of integers ‘freq’ denoting the number of coins of each denomination.You have to find the number of ways to make the sum ‘V’ by selecting some(or all) coins from the array.The answer can be very large. So, return the answer modulo 1000000007.For Example :‘N’ = 3, ‘coins’ = {1, 2, 3}, ‘freq’ = {1, 1, 3}, ‘V’ = 6For the given example, we can make six by using the following coins:{1, 2, 3}{3. 3}Hence, the answer is 2.
Solution
This problem can be solved using dynamic programming. Here are the steps:
-
Initialize a dp array of size V+1 with all elements as 0. The dp[i] will store the number of ways we can make the sum i using the given coins. dp[0] is 1 because there is one way to make the sum 0 i.e., by selecting no coins.
-
Iterate over each coin. For each coin, iterate from 1 to freq[i] (both inclusive) and for each j, iterate from V to coin[i]*j (both inclusive). For each iteration, add dp[V - coin[i]*j] to dp[V] and take modulo 1000000007 to avoid overflow.
-
The final answer will be dp[V].
Here is the Python code for the above steps:
def countWays(N, coins, freq, V):
mod = 1000000007
dp = [0]*(V+1)
dp[0] = 1
for i in range(N):
for j in range(1, freq[i]+1):
for v in range(V, coins[i]*j-1, -1):
dp[v] = (dp[v] + dp[v - coins[i]*j]) % mod
return dp[V]
In the given example, N = 3, coins = [1, 2, 3], freq = [1, 1, 3], V = 6. The output of countWays(N, coins, freq, V) will be 2.
Similar Questions
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.You may assume that you have an infinite number of each kind of coin. Example 1:Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1Example 2:Input: coins = [2], amount = 3Output: -1Example 3:Input: coins = [1], amount = 0Output: 0 Constraints:1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5. Sample Input : 10 (Value of N) 3 1 2 3 Sample Output : 5
You are given an integer array coins representing coins of different denominations and an integer k.You have an infinite number of coins of each denomination. However, you are not allowed to combine coins of different denominations.Return the kth smallest amount that can be made using these coins. Example 1:Input: coins = [3,6,9], k = 3Output: 9Explanation: The given coins can make the following amounts:Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc.Coin 6 produces multiples of 6: 6, 12, 18, 24, etc.Coin 9 produces multiples of 9: 9, 18, 27, 36, etc.All of the coins combined produce: 3, 6, 9, 12, 15, etc.Example 2:Input: coins = [5,2], k = 7Output: 12 Explanation: The given coins can make the following amounts:Coin 5 produces multiples of 5: 5, 10, 15, 20, etc.Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc.All of the coins combined produce: 2, 4, 5, 6, 8, 10, 12, 14, 15, etc. Constraints:1 <= coins.length <= 151 <= coins[i] <= 251 <= k <= 2 * 109coins contains pairwise distinct integers.Java 1class Solution {2 public long findKthSmallest(int[] coins, int k) {3 4 }5} Custom TestcaseUse Example Testcases Run Code SubmitCopyright © 2024 LeetCodeHelp CenterJobsBug BountyOnline InterviewStudentsTermsPrivacy PolicyUnited States
ou are making gift boxes. Each box contains 7 items.Given 200 items in total, you need to calculate and output how many items will be left over if you create gift boxes from all of the items.The modulo operator % can be useful in solving this challenge.
ix identical coins are arranged in a row. The number of ways in which the number of tails is equal to the number of heads is20912040
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.