Problem link : MAXDIFF
My code : MAXDIFF
Can’t figure out the error. Please help.
i will give you a hint (and solution if you want later):-
For the case
1 2 3 4 5 6 7 8 9 10.
Your code gives 35 but the
answer should be 54. now think why!!
@gautam94 : You need to consider two divisions .
Highest k in first division and other n-k in other division .
Lowest k in first division and other n-k in other division .
You are considering only one case .
Could not understand this statement
for (p < y - z; p >= 0; --p) first part of "for" loop should not be conditional but an assignment.
@gautam94 : You could try using long int instead of int . I submitted the problem correctly during contest time itself as it is a recent contest problem . However I did it in Java . The logic is correct now , but seems some other language issue .
WA even with
long int . http://ideone.com/LZnyKw
First of all, @kcahdog the output of
10 9 1 2 3 4 5 6 7 8 9 10
Second, long int and int are same in cpp and it doesn’t affect anything at all. Since, input constraints are small and int is itself enough to hold both input and output.
Third, there is something wrong in your code.
sort (a, a + y - 1)
you are using sort function which takes two argument array_begin, array_end. and if a is the begining element then a+y-1 can’t be the ending element it have to be a+y.
So, change your sort statement to this
And, it will work surely.
For the above case, the answer should be 53. Isn’t it?
http://ideone.com/pokFXV Still getting WA.
I changed that part to
for (p = y - z - 1; p >= 0; --p) but still WA.
Yep, my mistake.Had an exam today so was in a bit of a hurry. Good he realized it himself