- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion

# C/C++ Program for Greedy Algorithm to find Minimum number of Coins

A greedy algorithm is an algorithm used to find an optimal solution for the given problem. greedy algorithm works by finding locally optimal solutions ( optimal solution for a part of the problem) of each part so show the Global optimal solution could be found.

In this problem, we will use a *greedy algorithm to find the minimum number of coins/ notes that could makeup to the given sum.* For this we will take under consideration all the valid coins or notes i.e. denominations of { 1, 2, 5, 10, 20, 50 , 100, 200 , 500 ,2000 }. And we need to return the number of these coins/notes we will need to make up to the sum.

Let’s take a few examples to understand the context better −

#### Example 1 −

Input : 1231 Output : 7

**Explanation** − We will need two Rs 500 notes, two Rs 100 notes, one Rs 20 note, one Rs 10 note and one Re 1 coin. That sums to 2+2+1+1+1 = 7

#### Example 2 −

Input : 2150 Output : 3

Explanation − We will need one Rs 2000 note, one Rs 100 note, and one Rs 50 note.

To solve this problem using a greedy algorithm, we will find the which is the largest denomination that can be used. then we will subtract the largest denomination from the sum and again do the same process until the sum becomes zero.

## Algorithm

Input: sum, Initialise the coins = 0 Step 1: Find the largest denomination that can be used i.e. smaller than sum. Step 2: Add denomination two coins and subtract it from the Sum Step 3: Repeat step 2 until the sum becomes 0. Step 4: Print each value in coins.

## Example

#include <bits/stdc++.h> using namespace std; int notes[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 }; int n = sizeof(notes) / sizeof(notes[0]); void minchange(int sum){ vector<int> coins; for (int i = n - 1; i >= 0; i--) { while (sum >= notes[i]) { sum -= notes[i]; coins.push_back(notes[i]); } } for (int i = 0; i < coins.size(); i++) cout << coins[i] << "\t"; } int main(){ int n = 3253; cout << "The minimum number of coins/notes that sum up " << n << " is \t "; minchange(n); return 0; }

## Output

The minimum number of coins/notes that sum up 3253 is 2000 500 500 200 50 2 1

- Related Articles
- Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++
- C++ program to count number of minimum coins needed to get sum k
- Find out the minimum number of coins required to pay total amount in C++
- C++ program to find minimum how many coins needed to buy binary string
- C++ Program to Perform Greedy Coloring
- Python Program for Find minimum sum of factors of number
- C Program for Minimum number of jumps to reach the end
- Program to find number of coins needed to make the changes with given set of coins in Python
- Minimum number of coins that make a given value
- Program to find minimum number of rocketships needed for rescue in Python
- Program to find number of combinations of coins to reach target in Python
- C++ program to find minimum possible number of keyboards are stolen
- C Program to Find the minimum sum of factors of a number?
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of coins we can get using Python