See this code working fine for the sample test case but doesnot passing the test cases

HELLO @anshulgurawalia
pls explain ur approach

first i take the array of even size (n) then sort it in increasing order take first n/2 element from array make another small_arr1 having size n/2
then i again sort the array in decreasing order using comparators and take first n/2 elements from array and make another small_arr2 having size n/2
for the maximum difference simply subtract small_arr1[i] - small_arr2[ i ] for all elements of the where ( i goes from 0 to n/2)
for the minimum difference first i swap the maximum element from both the array ( small_arr1 and small_arr2) and again find the difference ( small_arr1[i] - small_arr2[ i ] )
for eg:-
first take arr[ ]:- 3,0 ,12,10
n=4;
after sort :- -3,0,10,12 -3,
make small_arr1[ n/2 ]={ -3,0} ; ( take n/2 element from arr)
again sort the arr[ ] in decreasing order now arr becomes: 12,10,0,-3
now make small_arr2[n/2]= {12,10} ( take n/2 element from arr)
form maxdiff= small_arr1[0]-small_arr2[0]+ small_arr1[1]-small_arr2[1];
= |-3-12| + |0-10|= 15+10=25;
now swap the highest element from small_arr1 with small_arr2
now small_arr1[ ]={ -3, 12} and small_arr2[ ]={ 0,10} ;
form mindiff= small_arr1[0]-small_arr2[0]+ small_arr1[1]-small_arr2[1];
=|-3-0| +|12-10|
= 3+ 2 =5

@anshulgurawalia
sort the array in ascending order
a)
for maximum , we want difference between every two pair is maximum , sp if we pick A[i] then other pair should be A[i+n/2]

compute A[i+n/2]-A[i] , i =[0…n/2-1]
b)
for minimum, we will pick adjacent element and compute its difference, because difference between adjacent is minimum possible
compute summation -> A[2i+1]-A[2i] ,i =[0…n/2-1]

can you please see this code its working fine or not i know it is not optimised

no its not.

your max diff2 is not correct, swapping maximum will ensure that u will get minimum .

pick adjacent pairs (of original sorted array) to minimise the diff

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.