plz check [MAXDIFF]

what was the error in this code???

http://www.codechef.com/viewsolution/1984132

Thereâ€™s 2 possibilities, and youâ€™ve got to check them both.

eg. K = 2, for list [1,2,3,â€¦,98,99,100]

either:

[1,2] [3,â€¦,98,99,100]

or:

[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

2 Likes

@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â€¦

http://www.codechef.com/viewsolution/1984132

@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

in addition to that if you would have done

if(k>(n/2))

k=n-k;

you would have got it accepted.

thanksâ€¦ now i got itâ€¦

This is the logic I also used and wanted to share. More precisely,
if(2*K>N){
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.

Hi
Can anyone check this code http://www.codechef.com/viewsolution/2059067 and tell me why am I getting â€śwrong answerâ€ť.
As I check, the output and input format and answers are exactly the same.
Thanks