Link ques:https://practice.geeksforgeeks.org/problems/swapping-pairs-make-sum-equal4142/1
Can you pls explain what is the error
if(A[i]+B[j]==diff){
return 1;
}
this condition inside while loop is not correct
Example
if sum1 is 10 and sum2 is 20
now diff is 10
if an element in arr1 is 9 and an element in arr2 is 1 both sum is 10 but if we swap them
then sum1 will be 2 and sum2 will be 28
but you return 1 which is incorrect
Correct approach
- Sort both arrays
- maintain two pointers i ( 0 to n) & j ( 0 to m) and variable prevsum = MIN
- check sum of arrays swapping i & j, return 1 if same
- if difference of above sum’s (say X ) is greater than its prevsum, increment j, update prevsum = X
- else increment i, set j=0 , update prevsum = MIN
Reference Code
i am not able to understand the lasst two if and else statement can you please explain it again

s1-s2 is the current difference
diff is previous difference
if current diff is greater than previous difference we have to swap greater no hence increase j
as array are sorted
else we have to swap smaller no so we increase i and set j to 0 also set diff to INT_MIN
means re-initialize the variables
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.