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:

Software Engineering Concepts Software Maintenance Software Maintenance Quiz 1

  Software Engineering Concepts  Software Maintenance  Software Maintenance Quiz 1

RDBMS Data Definition Language | Create Distributor table

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

RDBMS Data Definition Language Create | Customer_info table

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

Basics of Java | Operators | Day 1

Problem: Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition , while * is also an operator used for multiplication . Operators in Java can be classified into 6 types: Arithmetic Operators Assignment Operators Relational Operators Logical Operators Unary Operators Bitwise Operators References: Click Here . We will discuss about arithmetic operators and rest you can study from the references mentioned above. Arithmetic Operators: Arithmetic operators are used to perform arithmetic operations on variables and data. For example, a + b; Here, the + operator is used to add two variables a and b. Similarly, there are various other arithmetic operators in Java. Operator Operation + Addition - Subtraction * Multiplication / Division % Modulo Operation (Remainder after division) Task: You are given two integers as input a and b You need to perform several...

Data Formats ( XML & JSON ) XML AND JSON | Generate XSD For Mobile Store

  Generate XSD For Mobile Store <?xml version="1.0" encoding="UTF-8"?> <mobilestore> <mobile> <brand>Nokia</brand> <os>Symbian</os> <model>C6</model> <ram>1gb</ram> <internal>8gb</internal> </mobile> <mobile> <brand>Samsung</brand> <os>Android</os> <model>Galaxy</model> <ram>2gb</ram> <internal>8gb</internal> </mobile> <mobile> <brand>Sony</brand> <os>Android</os> <model>Experia</model> <ram>512mb</ram> <internal>16gb</internal> </mobile> </mobilestore> <? xml  version = "1.0"  encoding = "UTF-8" ?> < xs:schema   xmlns:xs = "http://www.w3.org/2001/XMLSchema"   elementFormDefault = "qualified"   attributeFormDefault = "unqualifi...

Software Engineering Concepts Introduction to Agile Technologies Introduction to Agile Technologies | Quiz 2

Software Engineering Concepts        Introduction to Agile Technologies             Introduction to Agile Technologies | Quiz 2

Cars and Bikes Problem Code: TYRES | CodeChef

Problem: Chef opened a company which manufactures cars and bikes. Each car requires   4 4  tyres while each bike requires  2 2  tyres. Chef has a total of  N N  tyres ( N N  is even). He wants to manufacture maximum number of cars from these tyres and then manufacture bikes from the remaining tyres. Chef's friend went to Chef to purchase a bike. If Chef's company has manufactured even a single bike then Chef's friend will be able to purchase it. Determine whether he will be able to purchase the bike or not. Input Format The first line contains an integer  T T  denoting the number of test cases. The  T T  test cases then follow. The first line of each test case contains an integer  N N  denoting the number of tyres. Output Format For each test case, output  YES  or  NO  depending on whether Chef's friend will be able to purchase the bike or not. Output is case insensitive. Constraints 1 ≤ T ≤ 100 1 ≤ T ≤...

Software Engineering Concepts Configuration Management And Version Control Configuration Management And Version Control Quiz 1

 

Climb Stairs With Variable Jumps

 1. You are given a number n, representing the number of stairs in a staircase. 2. You are on the 0th step and are required to climb to the top. 3. You are given n numbers, where ith element's value represents - till how far from the step you       could jump to in a single move.        You can of course jump fewer number of steps in the move. 4. You are required to print the number of different paths via which you can climb to the top. Input Format A number n .. n more elements Output Format A number representing the number of ways to climb the stairs from 0 to top. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 20 Sample Input 10 3 3 0 2 1 2 4 2 0 0 Sample Output 5 Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         // write your code here         Scanner sc = new Scanner(System.in);   ...

Subscribe to Get's Answer by Email