MAXDIFF: Which test cases am I failing?

Can you please tell me which test cases my code is throwing WA for MAXDIFF?
https://www.codechef.com/viewsolution/16190441

Access denied to your solution- please re-check link.

Anyways, one of the corner case was that -You have to sort the weights, and check which is giving more difference- taking first K elements or taking last K elements.

1 Like

I’v updated the link… I am sorting the array first and then checking the difference…

You are doing the same mistake which I stated.

The difference can be max by either taking first K elements, or last K elements.

Eg, take this example-

[1,1,1,1000].

For K=1, here the difference is greatest on taking only first element, diff=(1000+1+1)-1=1001. Now look at next example-

[1,1000,1000,1000,1000]. K=4.

Taking first 4 elements gives us 3001-1000=2001. Taking last 4 elements gives us 4000-1=3999.

I think you get the point now :slight_smile:

Yes THanks again!

what is wrong with my code here?
https://www.codechef.com/viewsolution/29022601