Skip to main content

Coin Change Combination

1. You are given a number n, representing the count of coins.

2. You are given n numbers, representing the denominations of n coins.

3. You are given a number "amt".

4. You are required to calculate and print the number of combinations of the n coins using which the 

     amount "amt" can be paid.

Note 1: You have an infinite supply of each coin denomination i.e. same coin denomination can be 

                  used for many installments in payment of "amt"

Note 2: You are required to find the count of combinations and not permutations i.e.

                  2 + 2 + 3 = 7 and 2 + 3 + 2 = 7 and 3 + 2 + 2 = 7 are different permutations of same 

                  combination. You should treat them as 1 and not 3.

Input Format

A number n

n1

n2

.. n number of elements

A number amt

Output Format

A number representing the count of combinations of coins which can be used to pay the amount "amt"


Constraints

1 <= n <= 30

0 <= n1, n2, .. n elements <= 20

0 <= amt <= 50

Sample Input

4

2

3

5

6

7

Sample Output

2


Solution:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        // input
        Scanner sc = new Scanner(System.in);
        int n =sc.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = sc.nextInt();
        int amt = sc.nextInt();
        int[] dp = new int[amt+1];
        // process
        dp[0] = 1;
        
        for(int i=0;i<n;i++){
            for(int j=1;j<=amt;j++){
                if(arr[i]<=j)
                    dp[j] += dp[j-arr[i]];
            }
        }
        
        // output
        System.out.println(dp[amt]);
    }
}

Comments

Must Read:

RDBMS Data Definition Language | Create Sales_info table

  Write a query to create Sales_info with constraints mentioned. Refer the below schema  Result Description Summary of tests +------------------------------+ | 3 tests run / 3 test passed | +------------------------------+

Algorithm Analysis and Design Concepts Data Structure and Algorithms Post-Quiz

Algorithm Analysis and Design Concepts       Data Structure and Algorithms            Post-Quiz  

Algorithm Analysis and Design Concepts Analysis of Algorithms Post-Quiz

 Algorithm Analysis and Design Concepts       Analysis of Algorithms            Post-Quiz  

RDBMS Data Definition Language | Change the name of the table Sales Info

  RDBMS  Data Definition Language  Change the name of the table Sales Info Write an SQL statement to rename the table Sales Info to Sales_Information. (Hint: use alter command to rename the table name) Evaluation Result: Result Description Summary of tests +------------------------------+ | 1 tests run / 1 test passed | +------------------------------+

Mock 3 Slot3 - Quiz - 21st July Quiz Quiz

Mock 3 Slot3        Quiz - 21st July          Important Questions

Logic Development | Object Oriented Programming Post Quiz

 Logic Development | Object-Oriented Programming | Post Quiz

Programming using Java Hands On - Control Structures | Print Username

  Print Username Jeffy, who is going to complete the higher education in this year needs to create a simple application which accept the name of a person and welcome them with a message along with their name. She wants to read the data using the class "Scanner". Implement this scenario using Java. Sample Input 1:   Enter the name: Johson Sample Output 1: Welcome Johnson. Sample Input 2:   Enter the name: Stain Polson Sample Output 2: Welcome Stain Polson. Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 2 tests run/ 2 tests passed | +------------------------------+

Logic Development | Object Oriented Programming Pre Quiz

 

Zig Zag Pattern

  /* @ToDo     Zig Zag Pattern         *               *                *       *       *       *        *               *               *        */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE          freopen ( "../asset/input.txt" , "r" , stdin );          freopen (...

Subscribe to Get's Answer by Email