Find out the minimum number of coins required to pay total amount
Given a total amount of N and unlimited number of coins worth 1, 10 and 25 currency coins. Find out the minimum number of coins you need to use to pay exactly amount N.
Examples:
Input : N = 14
Output : 5
You will use one coin of value 10 and
four coins of value 1.
Input : N = 88
Output : 7
Approach:
To solve this problem we will use recursion to try all possible combinations of coins and return the minimum count of coins required to make the given amount. We can start with the largest coin, i.e., 25, and then try all possible combinations of 25, 10, and 1 coins.
Let countCoins(n) be the minimum number of coins required to make the amount n. Then, countCoins(n) can be computed as follows:
countCoins(n) = min(countCoins(n-25), countCoins(n-10), countCoins(n-1)) + 1
The base cases are countCoins(0) = 0 (no coins required to make 0) and countCoins(n) = INT_MAX for n < 0 (no solution for negative amounts).
- Check if the amount n is 0. If yes, then no coins are required, so return 0.
- Check if the amount n is negative. If yes, then no solution exists, so return INT_MAX.
- If n is positive, then try all possible combinations of coins by making recursive calls for n-25, n-10, and n-1. Store the counts of coins returned by these recursive calls in count1, count2, and count3 respectively.
- Return the minimum of count1, count2, and count3 plus 1, as one coin will be required for the current value of n.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countCoins( int n)
{
if (n == 0) {
return 0;
}
if (n < 0) {
return INT_MAX;
}
int count1 = countCoins(n-25);
int count2 = countCoins(n-10);
int count3 = countCoins(n-1);
return min(count1, min(count2, count3)) + 1;
}
int main()
{
int n = 14;
cout << countCoins(n);
return 0;
}
|
Java
public class MinimumCoins {
public static int countCoins( int n) {
if (n == 0 ) {
return 0 ;
}
if (n < 0 ) {
return Integer.MAX_VALUE;
}
int count1 = countCoins(n - 25 );
int count2 = countCoins(n - 10 );
int count3 = countCoins(n - 1 );
return Math.min(count1, Math.min(count2, count3)) + 1 ;
}
public static void main(String[] args) {
int n = 14 ;
System.out.println(countCoins(n));
}
}
|
Python3
def countCoins(n):
if n = = 0 :
return 0
if n < 0 :
return float ( "inf" )
count1 = countCoins(n - 25 )
count2 = countCoins(n - 10 )
count3 = countCoins(n - 1 )
return min (count1, count2, count3) + 1
n = 14
print (countCoins(n))
|
C#
using System;
class Program
{
static int CountCoins( int n)
{
if (n == 0)
{
return 0;
}
if (n < 0)
{
return int .MaxValue;
}
int count1 = CountCoins(n - 25);
int count2 = CountCoins(n - 10);
int count3 = CountCoins(n - 1);
return Math.Min(count1, Math.Min(count2, count3)) + 1;
}
static void Main()
{
int n = 14;
Console.WriteLine(CountCoins(n));
}
}
|
Javascript
function countCoins(n) {
if (n === 0) {
return 0;
}
if (n < 0) {
return Number.MAX_SAFE_INTEGER;
}
const count1 = countCoins(n - 25);
const count2 = countCoins(n - 10);
const count3 = countCoins(n - 1);
return Math.min(count1, count2, count3) + 1;
}
const n = 14;
console.log(countCoins(n));
|
Time Complexity: O(3^n), where n is the amount.
Space Complexity: O(n), where n is the amount, due to the recursion depth.
Approach:
There are three different cases:
- If value of N < 10, then coins that have value 1 can only be used for payment.
- When N > 9 and < 25, then coins that have value 1 and 10 will be used for payment. Here, to minimize the number of coins used, coins with value 10 will be preferred mostly.
- When N > 24. Then all coins of value 1, 10 and 25 will be used for payment. To minimize the number of coins, the primary aim will be to use coin with value 25 first as much as possible then coin with value 10 and then with value 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countCoins( int n)
{
int c = 0;
if (n < 10) {
return n;
}
if (n > 9 && n < 25) {
c = n / 10 + n % 10;
return c;
}
if (n > 24) {
c = n / 25;
if (n % 25 < 10) {
c = c + n % 25;
return c;
}
if (n % 25 > 9) {
c = c + (n % 25) / 10 + (n % 25) % 10;
return c;
}
}
}
int main()
{
int n = 14;
cout << countCoins(n);
return 0;
}
|
Java
class GFG {
static int countCoins( int n)
{
int c = 0 ;
if (n < 10 ) {
return n;
}
if (n > 9 && n < 25 ) {
c = n / 10 + n % 10 ;
return c;
}
if (n > 24 ) {
c = n / 25 ;
if (n % 25 < 10 ) {
c = c + n % 25 ;
return c;
}
if (n % 25 > 9 ) {
c = c + (n % 25 ) / 10 + (n % 25 ) % 10 ;
return c;
}
}
return c;
}
public static void main(String[] args)
{
int n = 14 ;
System.out.println(countCoins(n));
}
}
|
Python3
def countCoins(n):
c = 0
if (n < 10 ):
return n
if (n > 9 and n < 25 ):
c = n / / 10 + n % 10
return c
if (n > 24 ):
c = n / / 25
if (n % 25 < 10 ):
c = c + n % 25
return c
if (n % 25 > 9 ):
c = (c + (n % 25 ) / / 10 +
(n % 25 ) % 10 )
return c
n = 14
print (countCoins(n))
|
C#
using System;
class GFG
{
static int countCoins( int n)
{
int c = 0;
if (n < 10)
{
return n;
}
if (n > 9 && n < 25)
{
c = n / 10 + n % 10;
return c;
}
if (n > 24)
{
c = n / 25;
if (n % 25 < 10)
{
c = c + n % 25;
return c;
}
if (n % 25 > 9)
{
c = c + (n % 25) / 10 + (n % 25) % 10;
return c;
}
}
return c;
}
public static void Main()
{
int n = 14;
Console.WriteLine(countCoins(n));
}
}
|
Javascript
<script>
function countCoins( n)
{
var c = 0;
if (n < 10)
{
return n;
}
if (n > 9 && n < 25)
{
c = n / 10 + n % 10;
return Math.trunc(c);
}
if (n > 24)
{
c = n / 25;
if (n % 25 < 10)
{
c = c + n % 25;
return Math.trunc(c);
}
if (n % 25 > 9)
{
c = c + (n % 25) / 10 + (n % 25) % 10;
return Math.trunc(c);
}
}
}
var n = 14;
document.write(countCoins(n));
</script>
|
PHP
<?php
function countCoins( $n )
{
$c = 0;
if ( $n < 10)
{
return $n ;
}
if ( $n > 9 && $n < 25)
{
$c = (int)( $n / 10 + $n % 10);
return $c ;
}
if ( $n > 24)
{
$c = (int)( $n / 25);
if ( $n % 25 < 10)
{
$c = $c + $n % 25;
return $c ;
}
if ( $n % 25 > 9)
{
$c = $c + ( $n % 25) / 10 + ( $n % 25) % 10;
return $c ;
}
}
return $c ;
}
$n = 14;
echo (countCoins( $n ));
|
Time Complexity : O(1)
Auxiliary Space : O(1)
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...