Find minimum number of currency notes and values that sum to given amount
Given an amount, find the minimum number of notes of different denominations that sum up to the given amount. Starting from the highest denomination note, try to accommodate as many notes as possible for a given amount.
We may assume that we have infinite supply of notes of values {2000, 500, 200, 100, 50, 20, 10, 5, 1}
Examples:
Input : 800
Output : Currency Count
500 : 1
200 : 1
100 : 1
Input : 2456
Output : Currency Count
2000 : 1
200 : 2
50 : 1
5 : 1
1 : 1
This problem is a simple variation of coin change problem. Here Greedy approach works as the given system is canonical (Please refer this and this for details)
Below is the program implementation to find the number of notes:
C++
#include <bits/stdc++.h>
using namespace std;
void countCurrency( int amount)
{
int notes[9] = { 2000, 500, 200, 100,
50, 20, 10, 5, 1 };
int noteCounter[9] = { 0 };
for ( int i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount % notes[i];
}
}
cout << "Currency Count ->" << endl;
for ( int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
cout << notes[i] << " : "
<< noteCounter[i] << endl;
}
}
}
int main()
{
int amount = 868;
countCurrency(amount);
return 0;
}
|
Python3
def countCurrency(amount):
notes = [ 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 ]
notesCount = {}
for note in notes:
if amount > = note:
notesCount[note] = amount / / note
amount = amount % note
print ( "Currency Count ->" )
for key, val in notesCount.items():
print (f "{key} : {val}" )
amount = 868
countCurrency(amount)
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static void countCurrency( int amount)
{
int [] notes = new int []{ 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 };
int [] noteCounter = new int [ 9 ];
for ( int i = 0 ; i < 9 ; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount % notes[i];
}
}
System.out.println( "Currency Count ->" );
for ( int i = 0 ; i < 9 ; i++) {
if (noteCounter[i] != 0 ) {
System.out.println(notes[i] + " : "
+ noteCounter[i]);
}
}
}
public static void main(String argc[]){
int amount = 868 ;
countCurrency(amount);
}
}
|
C#
using System;
public class GfG{
public static void countCurrency( int amount)
{
int [] notes = new int []{ 2000, 500, 200, 100, 50, 20, 10, 5, 1 };
int [] noteCounter = new int [9];
for ( int i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = amount / notes[i];
amount = amount % notes[i];
}
}
Console.WriteLine( "Currency Count ->" );
for ( int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
Console.WriteLine(notes[i] + " : "
+ noteCounter[i]);
}
}
}
public static void Main(){
int amount = 868;
countCurrency(amount);
}
}
|
PHP
<?php
function countCurrency( $amount )
{
$notes = array (2000, 500, 200, 100,
50, 20, 10, 5, 1);
$noteCounter = array (0, 0, 0, 0, 0,
0, 0, 0, 0, 0);
for ( $i = 0; $i < 9; $i ++)
{
if ( $amount >= $notes [ $i ])
{
$noteCounter [ $i ] = intval ( $amount /
$notes [ $i ]);
$amount = $amount % $notes [ $i ];
}
}
echo ( "Currency Count ->" . "\n" );
for ( $i = 0; $i < 9; $i ++)
{
if ( $noteCounter [ $i ] != 0)
{
echo ( $notes [ $i ] . " : " .
$noteCounter [ $i ] . "\n" );
}
}
}
$amount = 868;
countCurrency( $amount );
?>
|
Javascript
<script>
function countCurrency(amount)
{
let notes = [ 2000, 500, 200, 100, 50, 20, 10, 5, 1 ];
let noteCounter = Array(9).fill(0);
for (let i = 0; i < 9; i++) {
if (amount >= notes[i]) {
noteCounter[i] = Math.floor(amount / notes[i]);
amount = amount % notes[i];
}
}
document.write( "Currency Count ->" + "<br/>" );
for (let i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
document.write(notes[i] + " : "
+ noteCounter[i] + "<br/>" );
}
}
}
let amount = 868;
countCurrency(amount);
</script>
|
Output:
Currency Count ->
500 : 1
200 : 1
100 : 1
50 : 1
10 : 1
5 : 1
1 : 3
Time Complexity: O(1), as the algorithm has a fixed number of iterations (9) that does not depend on the size of the input.
Auxiliary Space: O(1), as the algorithm only uses a fixed amount of space to store the notes and note counters, which does not depend on the size of the input.
Last Updated :
09 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...