what was the error in this code???

# plz check [MAXDIFF]

**evandrix**#2

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]

**code_un_code**#5

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

**manasvi2001**#7

@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.

**manasvi2001**#9

@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.

**manasvi2001**#11

in addition to that if you would have done

if(k>(n/2))

k=n-k;

you would have got it accepted.

**shiwakant**#13

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.