Given an amount of n rupees and an unlimited supply of coins or notes of denominations {1, 2, 5, 10}. we have to find the minimum number of coins required to make up the given amount.
Examples:
Input: n= 39
Output: 6
Explanation: 39 can be formed using 3 coins of 10 rupees ,1 coin of 5 rupees and 2 coins of 2 rupees so minimum coins required are 6.
Input: n = 121
Output: 13
Explanation: 121 can be formed using 12 coins of 10 rupees and 1 coin of 1 rupees.
[Approach 1] Using Iterative Way - O(n) Time and O(1) Space
To minimize the number of coins required, we should always pick the largest possible denomination first and then move to smaller ones. This ensures that the total coins used are minimum.
Steps to solve the problem:
- Start from the largest denomination and check how many times it fits into the remaining amount.
- Subtract that value from the amount and increase the coin count accordingly.
- Move to the next smaller denomination and repeat the process until the amount becomes zero.
C++
#include <iostream>
#include<vector>
using namespace std;
int findMin(int n)
{
int count=0;
vector<int> denomination = { 1, 2, 5, 10 };
// Traverse through all denomination
for (int i = denomination.size() - 1; i >= 0; i--) {
// Find denominations
while (n >= denomination[i]) {
n -= denomination[i];
count++;
}
}
return count;
}
int main()
{
int n = 39;
cout<<findMin(n);
return 0;
}
Java
import java.util.List;
public class GfG {
// Function to find the minimum number of coins
public static int findMin(int n) {
// Traverse through all denominations in reverse order
int count = 0;
int[] denomination = {1, 2, 5, 10};
for (int i = denomination.length - 1; i >= 0; i--) {
// Find denominations
while (n >= denomination[i]) {
n -= denomination[i];
count++;
}
}
return count;
}
public static void main(String[] args) {
int n = 39;
System.out.println(findMin(n));
}
}
Python
def findMin(n):
count =0
denomination = [1, 2, 5, 10]
# Traverse through all denomination
for i in range(len(denomination) - 1, -1, -1):
# Find denominations
while n >= denomination[i]:
n -= denomination[i]
count +=1
return count
if __name__ == '__main__':
n = 39
print(findMin(n))
C#
using System;
using System.Collections.Generic;
class GfG {
static int findMin(int n) {
// Traverse through all denomination
int count = 0;
int[] arr = {1, 2, 5, 10};
for (int i = arr.Length - 1; i >= 0; i--) {
// Find denominations
while (n >= arr[i]) {
n -= arr[i];
count++;
}
}
return count;
}
static void Main() {
int n = 39;
Console.Write(findMin(n));
}
}
JavaScript
function findMin(n) {
// Traverse through all denomination
const denomination = [1, 2, 5, 10];
let count =0;
for (let i = denomination.length - 1; i >= 0; i--) {
// Find denominations
while (n >= denomination[i]) {
n -= denomination[i];
count += 1
}
}
return count;
}
// Driver Code
const n = 39;
console.log(findMin(n));
[Approach 2] Using Recursion - O(n) Time and O(n) Space
The idea is to minimize the number of coins recursively by always starting with the highest denomination, subtracting it from the amount, and moving to smaller denominations when it no longer fits, until the amount becomes zero.
C++
#include <iostream>
#include <vector>
using namespace std;
int getMin(int n, vector<int> denomination, int i)
{
// Base case: if amount becomes 0
if (n == 0)
return 0;
// If denomination is greater than remaining amount,
// move to next smaller denomination
if (denomination[i] > n)
return getMin(n, denomination, i - 1);
// Otherwise, use this denomination and continue
return 1 + getMin(n - denomination[i], denomination, i);
}
int findMin(int n)
{
vector<int> denomination = { 1, 2, 5, 10 };
return getMin(n, denomination, denomination.size() - 1);
}
int main()
{
int n = 39;
cout << findMin(n);
return 0;
}
Java
import java.util.Arrays;
public class Main {
public static int getMin(int n, int[] denomination, int i) {
// Base case: if amount becomes 0
if (n == 0)
return 0;
// If denomination is greater than remaining amount,
// move to next smaller denomination
if (i < 0 || denomination[i] > n)
return getMin(n, denomination, i - 1);
// Otherwise, use this denomination and continue
return 1 + getMin(n - denomination[i], denomination, i);
}
public static int findMin(int n) {
int[] denomination = { 1, 2, 5, 10 };
return getMin(n, denomination, denomination.length - 1);
}
public static void main(String[] args) {
int n = 39;
System.out.println(findMin(n));
}
}
Python
from typing import List
def getMin(n: int, denomination: List[int], i: int) -> int:
# Base case: if amount becomes 0
if n == 0:
return 0
# If denomination is greater than remaining amount,
# move to next smaller denomination
if i < 0 or denomination[i] > n:
return getMin(n, denomination, i - 1)
# Otherwise, use this denomination and continue
return 1 + getMin(n - denomination[i], denomination, i)
def findMin(n: int) -> int:
denomination = [1, 2, 5, 10]
return getMin(n, denomination, len(denomination) - 1)
if __name__ == '__main__':
n = 39
print(findMin(n))
C#
using System;
class Program
{
static int GetMin(int n, int[] denomination, int i)
{
// Base case: if amount becomes 0
if (n == 0)
return 0;
// If denomination is greater than remaining amount,
// move to next smaller denomination
if (i < 0 || denomination[i] > n)
return GetMin(n, denomination, i - 1);
// Otherwise, use this denomination and continue
return 1 + GetMin(n - denomination[i], denomination, i);
}
static int FindMin(int n)
{
int[] denomination = { 1, 2, 5, 10 };
return GetMin(n, denomination, denomination.Length - 1);
}
static void Main()
{
int n = 39;
Console.WriteLine(FindMin(n));
}
}
JavaScript
function getMin(n, denomination, i) {
// Base case: if amount becomes 0
if (n === 0)
return 0;
// If denomination is greater than remaining amount,
// move to next smaller denomination
if (i < 0 || denomination[i] > n)
return getMin(n, denomination, i - 1);
// Otherwise, use this denomination and continue
return 1 + getMin(n - denomination[i], denomination, i);
}
function findMin(n) {
const denomination = [1, 2, 5, 10];
return getMin(n, denomination, denomination.length - 1);
}
function main() {
const n = 39;
console.log(findMin(n));
}
main();
Greedy Algorithm to find minimum number of Coins
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem