Greedy Algorithm Tutorial
Last Updated :
23 Jul, 2025
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems.
An optimization problem can be solved using Greedy if the problem has the following property:
- At every step, we can make a choice that looks best at the moment, and we get the optimal solution to the complete problem.
- Some popular Greedy Algorithms are Fractional Knapsack, Dijkstra’s algorithm, Kruskal’s algorithm, Huffman coding and Prim’s Algorithm
- The greedy algorithms are sometimes also used to get an approximation for Hard optimization problems. For example, the Traveling Salesman Problem is an NP-Hard problem. A Greedy choice for this problem is to pick the nearest unvisited city from the current city at every step. These solutions don't always produce the best optimal solution but can be used to get an approximately optimal solution.
However, it's important to note that not all problems are suitable for greedy algorithms. They work best when the problem exhibits the following properties:
- Greedy Choice Property: The optimal solution can be constructed by making the best local choice at each step.
- Optimal Substructure: The optimal solution to the problem contains the optimal solutions to its subproblems.
Characteristics of Greedy Algorithm
Here are the characteristics of a greedy algorithm:
- Greedy algorithms are simple and easy to implement.
- They are efficient in terms of time complexity, often providing quick solutions. Greedy Algorithms are typically preferred over Dynamic Programming for the problems where both are applied. For example, Jump Game problem and Single Source Shortest Path Problem (Dijkstra is preferred over Bellman Ford where we do not have negative weights).
- These algorithms do not reconsider previous choices, as they make decisions based on current information without looking ahead.
These characteristics help to define the nature and usage of greedy algorithms in problem-solving.
Want to master Greedy algorithm and more? Check out our DSA Self-Paced Course for a comprehensive guide to Data Structures and Algorithms at your own pace. This course will help you build a strong foundation and advance your problem-solving skills.
How does the Greedy Algorithm works?
Greedy Algorithm solve optimization problems by making the best local choice at each step in the hope of finding the global optimum. It's like taking the best option available at each moment, hoping it will lead to the best overall outcome.
Here's how it works:
- Start with the initial state of the problem. This is the starting point from where you begin making choices.
- Evaluate all possible choices you can make from the current state. Consider all the options available at that specific moment.
- Choose the option that seems best at that moment, regardless of future consequences. This is the "greedy" part - you take the best option available now, even if it might not be the best in the long run.
- Move to the new state based on your chosen option. This becomes your new starting point for the next iteration.
- Repeat steps 2-4 until you reach the goal state or no further progress is possible. Keep making the best local choices until you reach the end of the problem or get stuck.
Example:
Let's say you have a set of coins with values [1, 2, 5, 10] and you need to give minimum number of coin to someone change for 39.
The greedy algorithm for making change would work as follows:
- Step-1: Start with the largest coin value that is less than or equal to the amount to be changed. In this case, the largest coin less than or equal to 39 is 10.
- Step- 2: Subtract the largest coin value from the amount to be changed, and add the coin to the solution. In this case, subtracting 10 from 39 gives 29, and we add one 10-coin to the solution.
Repeat steps 1 and 2 until the amount to be changed becomes 0.
Below is the illustration of above example:
C++
// C++ Program to find the minimum number of coins
// to construct a given amount using greedy approach
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minCoins(vector<int> &coins, int amount) {
int n = coins.size();
sort(coins.begin(), coins.end());
int res = 0;
// Start from the coin with highest denomination
for(int i = n - 1; i >= 0; i--) {
if(amount >= coins[i]) {
// Find the maximum number of ith coin
// we can use
int cnt = (amount / coins[i]);
// Add the count to result
res += cnt;
// Subtract the corresponding amount from
// the total amount
amount -= (cnt * coins[i]);
}
// Break if there is no amount left
if(amount == 0)
break;
}
return res;
}
int main() {
vector<int> coins = {5, 2, 10, 1};
int amount = 39;
cout << minCoins(coins, amount);
return 0;
}
C
// C Program to find the minimum number of coins
// to construct a given amount using greedy approach
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int *)b - *(int *)a);
}
int minCoins(int coins[], int n, int amount) {
// Sort the coins in descending order
qsort(coins, n, sizeof(int), compare);
int res = 0;
// Start from the coin with highest denomination
for(int i = 0; i < n; i++) {
if(amount >= coins[i]) {
// Find the maximum number of ith coin
// we can use
int cnt = (amount / coins[i]);
// Add the count to result
res += cnt;
// Subtract the corresponding amount from
// the total amount
amount -= (cnt * coins[i]);
}
// Break if there is no amount left
if(amount == 0)
break;
}
return res;
}
int main() {
int coins[] = {5, 2, 10, 1};
int n = sizeof(coins) / sizeof(coins[0]);
int amount = 39;
printf("%d", minCoins(coins, n, amount));
return 0;
}
Java
// Java Program to find the minimum number of coins
// to construct a given amount using greedy approach
import java.util.Arrays;
class GfG {
static int minCoins(int[] coins, int amount) {
int n = coins.length;
Arrays.sort(coins);
int res = 0;
// Start from the coin with highest denomination
for (int i = n - 1; i >= 0; i--) {
if (amount >= coins[i]) {
// Find the maximum number of ith coin we can use
int cnt = (amount / coins[i]);
// Add the count to result
res += cnt;
// Subtract the corresponding amount from
// the total amount
amount -= (cnt * coins[i]);
}
// Break if there is no amount left
if (amount == 0)
break;
}
return res;
}
public static void main(String[] args) {
int[] coins = {5, 2, 10, 1};
int amount = 39;
System.out.println(minCoins(coins, amount));
}
}
Python
# Python Program to find the minimum number of coins
# to construct a given amount using greedy approach
def minCoins(coins, amount):
n = len(coins)
coins.sort()
res = 0
# Start from the coin with highest denomination
for i in range(n - 1, -1, -1):
if amount >= coins[i]:
# Find the maximum number of ith coin we can use
cnt = amount // coins[i]
# Add the count to result
res += cnt
# Subtract the corresponding amount from the total amount
amount -= cnt * coins[i]
# Break if there is no amount left
if amount == 0:
break
return res
if __name__ == "__main__":
coins = [5, 2, 10, 1]
amount = 39
print(minCoins(coins, amount))
C#
// C# Program to find the minimum number of coins
// to construct a given amount using greedy approach
using System;
class GfG {
static int minCoins(int[] coins, int amount) {
int n = coins.Length;
Array.Sort(coins);
int res = 0;
// Start from the coin with highest denomination
for (int i = n - 1; i >= 0; i--) {
if (amount >= coins[i]) {
// Find the maximum number of ith coin we can use
int cnt = (amount / coins[i]);
// Add the count to result
res += cnt;
// Subtract the corresponding amount from the total amount
amount -= (cnt * coins[i]);
}
// Break if there is no amount left
if (amount == 0)
break;
}
return res;
}
static void Main() {
int[] coins = { 5, 2, 10, 1 };
int amount = 39;
Console.WriteLine(minCoins(coins, amount));
}
}
JavaScript
// JavaScript Program to find the minimum number of coins
// to construct a given amount using greedy approach
function minCoins(coins, amount) {
let n = coins.length;
coins.sort((a, b) => a - b);
let res = 0;
// Start from the coin with highest denomination
for (let i = n - 1; i >= 0; i--) {
if (amount >= coins[i]) {
// Find the maximum number of ith coin we can use
let cnt = Math.floor(amount / coins[i]);
// Add the count to result
res += cnt;
// Subtract the corresponding amount from the total amount
amount -= (cnt * coins[i]);
}
// Break if there is no amount left
if (amount === 0)
break;
}
return res;
}
// Driver code
let coins = [5, 2, 10, 1];
let amount = 39;
console.log(minCoins(coins, amount));
The greedy algorithm is not always the optimal solution for every optimization problem, as shown in the example below.
- When using the greedy approach to make change for the amount 20 with the coin denominations [18, 1, 10], the algorithm starts by selecting the largest coin value that is less than or equal to the target amount. In this case, the largest coin is 18, so the algorithm selects one 18 coin. After subtracting 18 from 20, the remaining amount is 2.
- At this point, the greedy algorithm chooses the next largest coin less than or equal to 2, which is 1. It then selects two 1 coins to make up the remaining amount. So, the greedy approach results in using one 18 coin and two 1 coins.
However, the greedy approach fails to find the optimal solution in this case. Although it uses three coins, a better solution would have been to use two 10 coins, resulting in a total of only two coins (10 + 10 = 20).
Related Articles:
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem