WA on 3 out of 6 cases in Chef and maximum Coloring CAMC

tc
{

ll n,m;
cin>>n>>m;
pair<ll,ll> arr[n];
fora(i,n)
    {

    cin>>arr[i].first;
    arr[i].second=i%m;


    }
    sort(arr,arr+n);
set<ll> s;
ll l=0,r=0;
s.insert(arr[l].second);
s.insert(arr[r].second);
ll ans=INT_MAX;
while(l<n&&r<n)
{
    if(s.size()==m)
    {   //cout<<arr[l].first<<" "<<arr[r].first<<endl;
        ans=min(ans,arr[r].first-arr[l].first);
        l++;
        s.erase(arr[l-1].second);
        s.insert(arr[l].second);
    } else
    {

        r++;
        s.insert(arr[r].second);


    }


}
cout<<ans<<endl;


};

This code seems fine to me and also my cases are matching with the setter’s code

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Assuming this is the code you mean, it fails on the testcase:

1
6 3
15 14 36 1 33 38 

The answer should be 21:

Best choice:
[ 15]  14 [ 36]   1 [ 33]  38 
  R    G    B    R    G    B  
max - min = 21
2 Likes

ssjgz can u tell me why my code is failing on this

thanks man!

1 Like

A std::set can’t contain duplicate elements. Try a std::multiset instead.

Edit: Sorry, a std::multiset won’t help, either - you need to keep a count of the number of each colour in the range, and use that to maintain a count of the number of different colours in the range.

1 Like

Sorry, my post was wrong - see my edit! :slight_smile:

1 Like

Is this spoonchef or something. ssjgz already provided a test case and you are too lazy to even find out why the hell it went wrong. It’s also a small test case too.

1 Like

sorry sir.

i was only asking if there was any small mistake in my code which can be modified. bt yes my logic was wrong

Thank you so much bro

1 Like