Dividing Array Concept

My approach is wrong may be because it’s giving wrong answer. Can anyone please tell, my approach is right or wrong!

Hey @Kinjal
Here you need to find minimum and maximum possible value for sum of abs(a[i]-b[i]) for i from 0 to n-1. Approach:

  1. for maximum, pair largest element with smallest, 2nd largest with second smallest and so on.
  2. for mimimum, pair smallest element with 2nd smallest or arr[i] with arr[i+1] for every i=0,2,4,6…
    try implementing this

So my greedy approach is like,

first step: sort the elements in the vector or in A[i] in decreasing order

second step:
a. to get maximum value, just divide it in half so that elements present in (0 to n/2-1) indexes present in A[i] and elements present in (n/2 to n-1) indexes are move to the B[i] and,

b. to get minimum value, odd indexes in A[i] and even indexes in B[i]

So I did it same way, I suppose. Can you kindly look at up?

1
3
12 -3 10 Check for this,ur code is not working for odd cases

And the output of this case should be??

Sorry my bad ,n will always be even (given in question),let me check ur code

Hey @Kinjal
Code is correct ,Your answer is overflowing,use long long instead of int for storing min and max

Okay bhaiya…thanks for updating.

All the test cases passed.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.