Given a value of V Rs and an infinite supply of each of the denominations {1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, The task is to find the minimum number of coins and/or notes needed to make the change?
Examples:
Input: V = 70
Output: 2
Explanation: We need a 50 Rs note and a 20 Rs note.
Input: V = 121
Output: 3
Explanation: We need a 100 Rs note, a 20 Rs note, and a 1 Rs coin.
Approach:
The intuition would be to take coins with greater value first. This can reduce the total number of coins needed. Start from the largest possible denomination and keep adding denominations while the remaining value is greater than 0.
Follow the steps below to implement the idea:
- Sort the array of coins in decreasing order.
- Initialize ans vector as empty.
- Find the largest denomination that is smaller than remaining amount and while it is smaller than the remaining amount:
- Add found denomination to ans. Subtract value of found denomination from amount.
- If amount becomes 0, then print ans.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int denomination[]
= { 1, 2, 5, 10, 20, 50, 100, 500, 1000 };
int n = sizeof (denomination) / sizeof (denomination[0]);
void findMin( int V)
{
sort(denomination, denomination + n);
vector< int > ans;
for ( int i = n - 1; i >= 0; i--) {
while (V >= denomination[i]) {
V -= denomination[i];
ans.push_back(denomination[i]);
}
}
for ( int i = 0; i < ans.size(); i++)
cout << ans[i] << " " ;
}
int main()
{
int n = 93;
cout << "Following is minimal"
<< " number of change for " << n << ": " ;
findMin(n);
return 0;
}
|
C
#include <stdio.h>
#define COINS 9
#define MAX 20
int coins[COINS] = { 1, 2, 5, 10, 20,
50, 100, 200, 2000 };
void findMin( int cost)
{
int coinList[MAX] = { 0 };
int i, k = 0;
for (i = COINS - 1; i >= 0; i--) {
while (cost >= coins[i]) {
cost -= coins[i];
coinList[k++] = coins[i];
}
}
for (i = 0; i < k; i++) {
printf ( "%d " , coinList[i]);
}
return ;
}
int main( void )
{
int n = 93;
printf ( "Following is minimal number"
"of change for %d: " ,
n);
findMin(n);
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static int deno[] = { 1 , 2 , 5 , 10 , 20 ,
50 , 100 , 500 , 1000 };
static int n = deno.length;
static void findMin( int V)
{
Vector<Integer> ans = new Vector<>();
for ( int i = n - 1 ; i >= 0 ; i--)
{
while (V >= deno[i])
{
V -= deno[i];
ans.add(deno[i]);
}
}
for ( int i = 0 ; i < ans.size(); i++)
{
System.out.print(
" " + ans.elementAt(i));
}
}
public static void main(String[] args)
{
int n = 93 ;
System.out.print(
"Following is minimal number "
+ "of change for " + n + ": " );
findMin(n);
}
}
|
Python3
def findMin(V):
deno = [ 1 , 2 , 5 , 10 , 20 , 50 ,
100 , 500 , 1000 ]
n = len (deno)
ans = []
i = n - 1
while (i > = 0 ):
while (V > = deno[i]):
V - = deno[i]
ans.append(deno[i])
i - = 1
for i in range ( len (ans)):
print (ans[i], end = " " )
if __name__ = = '__main__' :
n = 93
print ( "Following is minimal number" ,
"of change for" , n, ": " , end = "")
findMin(n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int []deno = { 1, 2, 5, 10, 20,
50, 100, 500, 1000 };
static int n = deno.Length;
static void findMin( int V)
{
List< int > ans = new List< int >();
for ( int i = n - 1; i >= 0; i--)
{
while (V >= deno[i])
{
V -= deno[i];
ans.Add(deno[i]);
}
}
for ( int i = 0; i < ans.Count; i++)
{
Console.Write( " " + ans[i]);
}
}
public static void Main(String[] args)
{
int n = 93;
Console.Write( "Following is minimal number " +
"of change for " + n + ": " );
findMin(n);
}
}
|
Javascript
<script>
let deno=[1, 2, 5, 10, 20,
50, 100, 500, 1000];
let n = deno.length;
function findMin(V)
{
let ans = [];
for (let i = n - 1; i >= 0; i--)
{
while (V >= deno[i])
{
V -= deno[i];
ans.push(deno[i]);
}
}
for (let i = 0; i < ans.length; i++)
{
document.write(
" " + ans[i]);
}
}
n = 93;
document.write(
"Following is minimal number "
+ "of change for " + n + ": " );
findMin(n);
</script>
|
Output
Following is minimal number of change for 93: 50 20 20 2 1
Time Complexity: O(V).
Auxiliary Space: O(V).
Note: The above approach may not work for all denominations.
For example, it doesn’t work for denominations {9, 6, 5, 1} and V = 11. The above approach would print 9, 1 and 1. But we can use 2 denominations 5 and 6.
For general input, below dynamic programming approach can be used: Find minimum number of coins that make a given value
Thanks to Utkarsh for providing the above solution here.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Minimum number of Coins using Ladder If-Else approach:
In this approach, we will simply iterate through the greater to smaller coins until the n is greater to that coin and decrement that value from n afterward using ladder if-else and will push back that coin value in the vector.
Follow the steps below to implement the idea:
- Declare a vector that store the coins.
- while n is greater than 0 iterate through greater to smaller coins:
- if n is greater than equal to 2000 than push 2000 into the vector and decrement its value from n.
- else if n is greater than equal to 500 than push 500 into the vector and decrement its value from n.
- And so on till the last coin using ladder if else.
- return the vector/array
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findMin( int n)
{
vector< int > v;
while (n > 0) {
if (n >= 2000) {
v.push_back(2000);
n -= 2000;
}
else if (n >= 500) {
v.push_back(500);
n -= 500;
}
else if (n >= 200) {
v.push_back(200);
n -= 200;
}
else if (n >= 100) {
v.push_back(100);
n -= 100;
}
else if (n >= 50) {
v.push_back(50);
n -= 50;
}
else if (n >= 20) {
v.push_back(20);
n -= 20;
}
else if (n >= 10) {
v.push_back(10);
n -= 10;
}
else if (n >= 5) {
v.push_back(5);
n -= 5;
}
else if (n >= 2) {
v.push_back(2);
n -= 2;
}
else if (n >= 1) {
v.push_back(1);
n -= 1;
}
}
return v;
}
int main()
{
int v = 93;
cout << "Following is minimal"
<< " number of change for " << v << ": " ;
vector< int > vec = findMin(v);
for ( auto it : vec)
cout << it << " " ;
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static Vector<Integer> findMin( int n){
Vector<Integer> vec = new Vector<>();
while (n > 0 ) {
if (n >= 2000 ) {
vec.addElement( 2000 );
n -= 2000 ;
}
else if (n >= 500 ) {
vec.addElement( 500 );
n -= 500 ;
}
else if (n >= 200 ) {
vec.addElement( 200 );
n -= 200 ;
}
else if (n >= 100 ) {
vec.addElement( 100 );
n -= 100 ;
}
else if (n >= 50 ) {
vec.addElement( 50 );
n -= 50 ;
}
else if (n >= 20 ) {
vec.addElement( 20 );
n -= 20 ;
}
else if (n >= 10 ) {
vec.addElement( 10 );
n -= 10 ;
}
else if (n >= 5 ) {
vec.addElement( 5 );
n -= 5 ;
}
else if (n >= 2 ) {
vec.addElement( 2 );
n -= 2 ;
}
else if (n >= 1 ) {
vec.addElement( 1 );
n -= 1 ;
}
}
return vec;
}
public static void main(String[] args)
{
int n = 93 ;
System.out.print(
"Following is minimal number "
+ "of change for " + n + ": " );
Vector<Integer> vec = findMin(n);
for (Integer i = 0 ; i < vec.size(); i++)
{
System.out.print(vec.get(i) + " " );
}
}
}
|
Python3
def findMin(n):
v = []
while (n > 0 ):
if (n > = 2000 ):
v.append( 2000 )
n - = 2000
elif (n > = 500 ):
v.append( 500 )
n - = 500
elif (n > = 200 ):
v.append( 200 )
n - = 200
elif (n > = 100 ):
v.append( 100 )
n - = 100
elif (n > = 50 ):
v.append( 50 )
n - = 50
elif (n > = 20 ):
v.append( 20 )
n - = 20
elif (n > = 10 ):
v.append( 10 )
n - = 10
elif (n > = 5 ):
v.append( 5 )
n - = 5
elif (n > = 2 ):
v.append( 2 )
n - = 2
elif (n > = 1 ):
v.append( 1 )
n - = 1
return v
v = 93
print ( "Following is minimal number of change for " , v, ":" )
vec = findMin(v)
for it in vec:
print (it, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< int > findMin( int n)
{
List< int > lst = new List< int >();
while (n > 0) {
if (n >= 2000) {
lst.Add(2000);
n -= 2000;
}
else if (n >= 500) {
lst.Add(500);
n -= 500;
}
else if (n >= 200) {
lst.Add(200);
n -= 200;
}
else if (n >= 100) {
lst.Add(100);
n -= 100;
}
else if (n >= 50) {
lst.Add(50);
n -= 50;
}
else if (n >= 20) {
lst.Add(20);
n -= 20;
}
else if (n >= 10) {
lst.Add(10);
n -= 10;
}
else if (n >= 5) {
lst.Add(5);
n -= 5;
}
else if (n >= 2) {
lst.Add(2);
n -= 2;
}
else if (n >= 1) {
lst.Add(1);
n -= 1;
}
}
return lst;
}
public static void Main()
{
int n = 93;
Console.Write( "Following is minimal number "
+ "of change for " + n + ": " );
List< int > lst = findMin(n);
for ( int i = 0; i < lst.Count; i++) {
Console.Write(lst[i] + " " );
}
}
}
|
Javascript
function findMin(n) {
let v = [];
while (n > 0) {
if (n >= 2000) {
v.push(2000);
n -= 2000;
} else if (n >= 500) {
v.push(500);
n -= 500;
} else if (n >= 200) {
v.push(200);
n -= 200;
} else if (n >= 100) {
v.push(100);
n -= 100;
} else if (n >= 50) {
v.push(50);
n -= 50;
} else if (n >= 20) {
v.push(20);
n -= 20;
} else if (n >= 10) {
v.push(10);
n -= 10;
} else if (n >= 5) {
v.push(5);
n -= 5;
} else if (n >= 2) {
v.push(2);
n -= 2;
} else if (n >= 1) {
v.push(1);
n -= 1;
}
}
return v;
}
let v = 93;
console.log( "Following is minimal number of change for " + v + " :" );
let vec = findMin(v);
console.log(vec.join( " " ));
|
Output
Following is minimal number of change for 93: 50 20 20 2 1
Time Complexity: O(N) that is equal to the amount v.
Auxiliary Space: O(1) that is optimized
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Feb, 2023
Like Article
Save Article