# Greedy Algorithm

A Greedy algorithm is one of the problem-solving methods which takes optimum solution in each mistreat .
The Greedy algorithm attempts to take the best in each step and it does n’t care about the overall consequence.

Greedy Approach – “ living in the stage. Do n’t overthink about the future ” .

Let ‘s discus avid overture with minimum mint change trouble .

## Minimum Coin Change Problem

Given a fit of coins and a prize, we have to find the minimum count of coins which satisfies the value .

#### Example

coins [ ] = { 5,10,20,25 }
prize = 50

### Possible Solutions

{ mint * count }
{ 5 * 10 } = 50 [ 10 coins ]
{ 5 * 8 + 10 * 1 } = 50 [ 9 coins ] goes on .
{ 10 * 5 } = 50 [ 5 coins ]
{ 20 * 2 + 10 * 1 } = 50 [ 3 coins ]
{ 20 * 2 + 5 * 2 } = 50 [ 4 coins ]
{ 25 * 2 } = 50 [ 2 coins ]
etc etc

### Best Solution

Two 25 rupees. entire coins two .
25 * 2 = 50

## Minimum Coin Change Problem Algorithm

1. Get coin array and a value .
2. Make sure that the array is sorted .
3. Take coin [ i ] angstrom much we can .
4. Increment the count .
5. If solution found ,
& nbspbreak it .
6. differently ,
follow step 3 with the adjacent mint. coin [ i+1 ] .
4. finally, print the count .

#### Example

coin [ ] = { 25,20,10,5 }
value = 50
Take coin [ 0 ] twice. ( 25+25 = 50 ) .
sum coins = 2 ( 25+25 )

#### Example

coin [ ] = { 25,20,10,5 }
value = 70
Take coin [ 0 ] twice. ( 25+25 = 50 ) .
If we take mint [ 0 ] one more time, the end consequence will exceed the given value. So, change the future mint .
Take coin [ 1 ] once. ( 50 + 20 = 70 ) .
entire coins needed = 3 ( 25+25+20 ) .

In this approach, we are not bothering about the overall result .
We equitable pick the best option in each tone and hoping that it might produce the best overall consequence .
therefore, this method acting called as the avaricious access .

## Implementation of minimum coin change problem

example

``` # include
# define soap 100

//arr - will have tilt of necessitate coins
int ans[max];

int  findMinCoins( int coins[],  int size,   int value)
{
int i, count  =  0;

for(i  =  0; i  < size; i ++ Read more: Bybit Learn | A Beginner’s Guide: What Is Shiba Inu (SHIB) Coin? )
{
//take as much from coins [ i ]
while(value  > = coins[i])
{
//after taking the coin, reduce the respect .
value  -= coins[i];
ans[count]  = coins[i];
count ++;
}
if(value  ==  0)
fracture;

}

return key count;
}

int  independent()
{
int coins[]  = { 25, 20, 10, 5};
int value  =  105, i;

//find the size of the coins array
int size  =  sizeof(coins) / sizeof(coins[ 0]);

int MinCount  = findMinCoins(coins,size,value);

printf( `` sum Coins Needed = % five hundred \n ``,MinCount);

printf( `` Coins are : \t ``);
for(i  =  0; i  < MinCount; i ++)
printf( `` % vitamin d ``, ans[i]);

return  0;
}
```

## When does the above greedy algorithm fail?

Let 's take this scenario .
coins [ ] = { 25,20,10,5 }
value = 40

The avid solution will be 25+10+5 [ 3 coins ] .
But the optimum solution will be 20+20 [ 2 coins ] .
so, we ca n't guarantee that the avaricious algorithm always produces the overall best leave .

## Topics You Might Like

reservoir : https://ontopwiki.com
Category : Finance