Cooling Pies Different approach

Problem link : http://www.codechef.com/problems/COOLING

Code link : http://www.codechef.com/viewsolution/2946610

What I have done different here, is that after sorting the arrays in ascending order, I have started i and j from the end. (In other words, I have moved in descending order).

pw[] is the pan weight array
mw[] is the max weight array

Initially, i = last element of pw
j = last element of mw

if pw[i] is greater than mw[j], then that pie is scrapped, and move on to the next largest pie weight. (by i–)
if however, pw[i] is less than or equal to mw[j], then we move on to the next largest pie weight and next largest max pie weight. (i-- and j–).

I am unable to figure out why my code is getting a WA, though the given test cases are satisfied.

1 Like

Logic is correct…good job!

you’ve just missed out 1 statement

This is your main loop… you need to put the i-- statement…i’ll tell you why

for(i=n-1,j=n-1;i>=0 && j>=0;)
    {
        if(pw[i]<=mw[j])
        {
            count++;
            j--;
            i--; //<----
        }
        
        else
        {
            i--;
        }
    }

Because once a pie finds a rack…you not only need to move to smaller rack but also to a lighter pie because that current pie has been placed in a rack… hope you get it

Cheers

1 Like

Thanks a lot!
Wonder how I missed out on this.