An Array of integers is given, both +ve and -ve. You need to find the two elements such that their sum is closest to zero.
METHOD 2 (Use Sorting)
1) Sort all the elements of the input array.
2) Use two index variables l and r to traverse from left and right ends respectively. Initialize l as 0 and r as n-1.
3) sum = a[l] + a[r]
4) If sum is -ve, then l++
5) If sum is +ve, then r–
6) Keep track of abs min sum.
7) Repeat steps 3, 4, 5 and 6 while l < r
Read full article from Two elements whose sum is closest to zero | GeeksforGeeks
METHOD 2 (Use Sorting)
1) Sort all the elements of the input array.
2) Use two index variables l and r to traverse from left and right ends respectively. Initialize l as 0 and r as n-1.
3) sum = a[l] + a[r]
4) If sum is -ve, then l++
5) If sum is +ve, then r–
6) Keep track of abs min sum.
7) Repeat steps 3, 4, 5 and 6 while l < r
void
minAbsSumPair(
int
arr[],
int
n)
{
// Variables to keep track of current sum and minimum sum
int
sum, min_sum = INT_MAX;
// left and right index variables
int
l = 0, r = n-1;
// variable to keep track of the left and right pair for min_sum
int
min_l = l, min_r = n-1;
/* Array should have at least two elements*/
if
(n < 2)
{
printf
(
"Invalid Input"
);
return
;
}
/* Sort the elements */
quickSort(arr, l, r);
while
(l < r)
{
sum = arr[l] + arr[r];
/*If abs(sum) is less then update the result items*/
if
(
abs
(sum) <
abs
(min_sum))
{
min_sum = sum;
min_l = l;
min_r = r;
}
if
(sum < 0)
l++;
else
r--;
}
}