Maximum difference

In this problem ,why can’t we just sort the weights and subtract the minimum among the sum of k/n-k weights from the maximum among the sum of n/n-k weights…

question-link text

I am not sure what you mean by n/n-k and n/n-k?
Are you using absolute value when calculating the difference?

The idea is as follows:

  • Sort the array
  • The answer is MAX(abs((sum of first k)-(the rest)), abs(sum of last k elements)-(the rest))
1 Like

I just code it for you.Here is the link: https://www.codechef.com/viewsolution/14347343

I think you wanna say something like,

result = max(sum of k, sum of n-k) - min(sum of k,sum of n-k)

That’s wrong because it’s not sure that K elements are from starting of the array, and it’s not sure that chef’s son always takes k elements.Read problem statement carefully and you will understand it.

Post problem link along with your query

n or n-k…n-k or k

my approach-
-sort
-print abs(sum 0f first k elements-sum 0f last n-k elements)

ok…understood …thanks bro!!!

Counter example:
a[] = {1,1,3}
k = 2

Your approach would get abs((1+1) - (3)) = 1
The correct answer in this case is to take the last k elements abs((1) - (1+3)) = 3