https://www.codechef.com/problems/MAXDIFF

https://www.codechef.com/viewsolution/26866025
can anyone please tell me why this is not accepting? Although i passed all the test cases.

Two mistakes:

  1. Range of int is only from -32767 to 32767. The constraints for the array elements in the problem are 100000. “long int” should be sufficient for it.

  2. You only test for one case. You have to consider k items from both sides of sorted array. For example, try this case:

5 4
1 1 1 1 9

Your program will divide this in groups as
(1, 1, 1, 1) and (9)
and give output of maxdiff as 9-4=5

The case you miss is
(9, 1, 1, 1) and (1)
Maxdiff is 12-1=11

You have to output max(5,11) that is the maximum value from the two answers.
I have updated your solution to work in the link below, but I encourage you to try it yourself first.

Hope I made it clear for you.
https://www.codechef.com/viewsolution/26866391

I guess you have made a mistake in your first point,

The range of int data type is from -2^31 to 2^31 - 1, and it would fit in the given constraints.

1 Like

Oh sorry my bad. While practicing C during my FE days, it was 2 bytes for int. Now I mostly code in Python so wasn’t sure about those.

1 Like

No problem, everyone makes mistakes.

1 Like