# Dedicate Length ### CONCEPTS USED:

Greedy algorithm.

Medium.

### PROBLEM STATEMENT`(`SIMPLIFIED`)`:

Nikhil wants to bring sofa(s) to his room. But he wants to dedicate the entire length of the room to the sofa(s) (yes I know he is a bit weird).
Now Nikhil’s room length is W meters, and when he went to the shop he found out sofas of two types, one of length N and other of length M.Now, let Nikhil know how many sofas of the first and second type he should buy to reduce wastage of space. First minimize the space wastage then, if similar result arises always prefer the sofa with a larger length. In case N==M give preference to second sofa.

See original problem statement here

#### For Example :

``````Input
1
24 3 5

Output
3 3

Nikhil will buy 3 sofas of size 3 and 3 sofa's of size 5.``````

### SOLVING APPROACH:

Since we have to minimize the waste of space, we shall find all possible arrangements and then choose the best one (greedy,right?).

Now you must be thinking that would take exponential time. But no, it won’t.

We shall keep storing the possible combination of each wasted space possible.

Then take the least one!!

### SOLUTIONS:

```#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
bool cmp1(pair<ll,ll> a,pair<ll,ll> b)
{
if(a.first==b.first)
return a.second>b.second;
return a.first>b.first;
}

bool cmp2(pair<ll,ll> a,pair<ll,ll> b)
{
if(a.second==b.second)
return a.first>b.first;
return a.second>b.second;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
ll w,n,m;
cin>>w>>n>>m;
vector<pair<ll,ll> >v[w+1];
ll so=1;
if(n>m)
{
so=0;
}
for(ll i=w/m;i>=0;i--)
{
ll l=w-(m*i);
v[l%n].push_back({l/n,i});

}
ll f=1;
for(ll i=0;i<=w;i++)
{
if(v[i].size()>0)
{
f=0;
if(so==0)
sort(v[i].begin(),v[i].end(),cmp1);
else
sort(v[i].begin(),v[i].end(),cmp2);
cout<<v[i].first<<" "<<v[i].second<<"\n";
break;

}

}
if(f==1)
cout<<"0 0\n";
}
return 0;
}
```

[forminator_quiz id="1473"]

This article tried to discuss Greedy Algorithm. Hope this blog helps you understand and solve the problem. To practice more problems on Greedy Algorithm you can check out MYCODE | Competitive Programming.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6823483,"math_prob":0.9480089,"size":2069,"snap":"2022-40-2023-06","text_gpt3_token_len":604,"char_repetition_ratio":0.10217918,"word_repetition_ratio":0.0,"special_character_ratio":0.30497825,"punctuation_ratio":0.18613862,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9928192,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-05T13:04:17Z\",\"WARC-Record-ID\":\"<urn:uuid:3dcffb94-8177-4a94-9a6e-ece9882cb9d4>\",\"Content-Length\":\"128276\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d8230a1e-a6f6-40a3-9594-cc1077606130>\",\"WARC-Concurrent-To\":\"<urn:uuid:28e02b29-cd5d-476a-b373-35849fbe29a8>\",\"WARC-IP-Address\":\"18.160.18.47\",\"WARC-Target-URI\":\"https://www.prepbytes.com/blog/greedy-algo-competitive-coding/dedicate-length/\",\"WARC-Payload-Digest\":\"sha1:GXGLWJVKWUC7RPATVP6EN24EBGK3326R\",\"WARC-Block-Digest\":\"sha1:7JRTF56RO7GTE3GWPYXI3KASKW3I5PGY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500255.78_warc_CC-MAIN-20230205130241-20230205160241-00275.warc.gz\"}"}