plz check [MAXDIFF]

what was the error in this code???

There’s 2 possibilities, and you’ve got to check them both.

eg. K = 2, for list [1,2,3,…,98,99,100]


[1,2] [3,…,98,99,100]


[99,100] [1,2,3,…,98]

@evandrix: not clear sir…

@evandrix: still not clear sir…

after scanning b and c, add if(c>(b-c)) c=b-c;
That should get you AC :slight_smile:


@hitesh091: Take for example the case

K=2 and list is 1 1 1 1 3

the two possible arrangements would be (1)+(1,1,1,3) and (1,1,1,1)+(3)

you have to take the one with maximum difference.

here, in first case the difference is 5 and in the second case, difference is 1. so, you select the first arrangement.

1 Like

@manasvi: am i wrong with my logic…

@hitesh091: yes, there is an error in your logic. the error being, you are only considering first k small elements.

Now consider this example.

let K=4 and the list be 1 1 1 1 9.

the ways your program is running is, it is picking first 4 elements for the kid, thus making the distribution 4 for the kid and 9 for the chef. Difference = 5.

but the correct way of doing this would be giving the last 4 elements to the chef and first element to the kid thus the distribution would be 1 for kid and 12 for the chef. Difference = 11.

1 Like

@hitesh091: my solution

in addition to that if you would have done



you would have got it accepted.

thanks… now i got it… :slight_smile:

This is the logic I also used and wanted to share. More precisely,
K = N - K;
Meanwhile try to use some meaningful variable names like the ones given in codechef questions.

@aichemzee suggested the changes to @hitesh091 code who used such weird names.

1 Like

^exactly. As in general you could do k=Math.min(k,n-k); to get AC.

Can anyone check this code and tell me why am I getting “wrong answer”.
As I check, the output and input format and answers are exactly the same.