0% found this document useful (0 votes)
17 views15 pages

Greedy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

Greedy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

GREEDY APPROACH

Programming Club - AKGEC


Greedy Approach
A greedy algorithm is an approach for solving a
problem by selecting the best option available at
the moment.

We build the solution piece by piece where each


piece is the most optimal solution at that instant.
A greedy algorithm doesn't worry whether the
current best result will bring the overall optimal
result. The algorithm never reverses the earlier
decision even if the choice is wrong.

So, when using a greedy approach, we need to make


sure that the local optimal solution obtained by the
greedy approach is also the global optimal one.
An example
Q) There are n items to be added in a box. We
have m (m>=n) items with different values. We
need to maximize the value of the box (value of
the box is sum of all items in the box).

Sol) A greedy approach would be to add the items


one by one with the largest value until we reach n
items.
Another example
Q) You are given an array of size n.
In one move, u can either:
i) Remove the first element from the array (or)
ii) Remove the last element from the array

Find the maximum sum of the removed elements


obtained after doing this operation exactly k times (k ≤
n).
Sample
a= 6 5 1 5 1 2 10
k=3
First step :- Remove the last element = 10
Second step :- Remove the first element = 6
Third step :- Remove the first element = 5 (First
element is now changed)
The sum of removed elements = 21
This can be proved to be the maximum sum,
since we removed the three greatest elements
Let us try to solve this problem greedily i.e;

for(k steps)
{
if(first element > last element)
remove first element
else
remove last element
}
ll i=0,j=n-1;
Code And ll ans =0;

Logic while(k>0){
if(v[i]>=v[j]){
ans+=v[i];
Here we use the two pointer - i++;
We would compare the first and }
last element in the array and else{
this would result to our answer . ans+=v[j];
j--;
}
k--;
}
cout<<ans<<"\n";
Does greedy work always ??

NO!!!!!!!!!!!!!
!
Consider the case:-
a= 1 100 3 4 5 6

k=2
Apply the greedy algorithm
i) Step 1:- last = 6 && first = 1; so remove last
=6
ii) Step 2:- last = 5 && first = 1; so remove last
=5
But, by inspection we can remove the first two elements =
Sum of removed numbers = 6 + 5 = 11
1 & 100
Now, sum of removed elements = 101
Observation
After removing k elements, we will have a subarray of length n -
k.
Now,
Sum of removed elements + Sum of the remaining subarray =
Sum of all elements

We have to maximise sum of removed elements; so we have to


minimize the sum of the remaining subarray;
Goal :- Find a subarray of length n -
k with minimum sum. ll n,k;
cin>>n>>k;
vector<ll> v(n);
Here we uses the for(int i=0;i<n;i++)cin>>v[i];
sliding window ll sum=0,sum1=0;
ll a=n-k;
technique to solve the for(int i=0;i<a;i+
problem. +)sum+=v[i];
for(int
Using it we able to i=0;i<n;i+)sum1+=v[i];
ll ans=sum;
find the subarray of for(int i=a;i<n;i++){
length n-k with sum-=v[i-a];
sum+=v[i];
minimum sum ans=min(ans,sum);
}
cout<<sum1-ans<<'\n';
MINIMIZING COIN
Consider a money system consisting of n coins. Each
coin has a positive integer value. Your task is to
produce a sum of money x using the available coins in
such a way that the number of coins is minimal.

20 50 100 200
N=
X= 520
Here the required money can be generated by
taking {200,200,100,20}
Sample-
N= 1 3 4

X=6

Here if we go by our greedy algorithm,then it


produces the solution 4+1+1 while the optimal
solution is 3+3.
Now ,it is now known that it can’t be solved by using
the greedy ,so on further lecture , we will see
That it is efficiently solved using dynamic programming
THANK YOU!

You might also like