Two elements whose sum is closest to zero | C ++ implementation using STL ; Modified to sort by abolute values ; Absolute value shows how close it is to zero ; if found an even close value update min and store the index ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( int x , int y ) { return abs ( x ) < abs ( y ) ; } void findMinSum ( int arr [ ] , int n ) { sort ( arr , arr + n , compare ) ; int min = INT_MAX , x , y ; for ( int i = 1 ; i < n ; i ++ ) { if ( abs ( arr [ i - 1 ] + arr [ i ] ) <= min ) { min = abs ( arr [ i - 1 ] + arr [ i ] ) ; x = i - 1 ; y = i ; } } cout << " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " << arr [ x ] << " ▁ and ▁ " << arr [ y ] ; } int main ( ) { int arr [ ] = { 1 , 60 , -10 , 70 , -80 , 85 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinSum ( arr , n ) ; return 0 ; }