C++ program:
#include <bits/stdc++.h>
using namespace std ; void getMinimumSum ( int arr [ ] , int n ) { vector < int > res ; vector < int > pos ; vector < int > neg ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos . push_back ( i ) ; else if ( arr [ i ] < 0 ) neg . push_back ( i ) ; } if ( pos . size ( ) >= 2 && neg . size ( ) >= 2 ) { int posMax = INT_MIN , posMaxIdx = -1 ; int posMin = INT_MAX , posMinIdx = -1 ; int negMax = INT_MIN , negMaxIdx = -1 ; int negMin = INT_MAX , negMinIdx = -1 ; for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ i ] ; negMin = abs ( arr [ negMinIdx ] ) ; } } double posVal = -1.0 * ( double ) posMax / ( double ) posMin ; double negVal = -1.0 * ( double ) negMax / ( double ) negMin ; if ( posVal < negVal ) { res . push_back ( arr [ posMinIdx ] ) ; res . push_back ( arr [ posMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . push_back ( arr [ i ] ) ; } } } else { res . push_back ( arr [ negMinIdx ] ) ; res . push_back ( arr [ negMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != negMinIdx && i != negMaxIdx ) { res . push_back ( arr [ i ] ) ; } } } for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << res [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; } else if ( pos . size ( ) >= 2 ) { int posMax = INT_MIN , posMaxIdx = -1 ; int posMin = INT_MAX , posMinIdx = -1 ; for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } res . push_back ( arr [ posMinIdx ] ) ; res . push_back ( arr [ posMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . push_back ( arr [ i ] ) ; } } for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << res [ i ] << " ▁ " ; } cout << " STRNEWLINE " ; } else if ( neg . size ( ) >= 2 ) { int negMax = INT_MIN , negMaxIdx = -1 ; int negMin = INT_MAX , negMinIdx = -1 ; for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ i ] ; negMin = abs ( arr [ negMinIdx ] ) ; } } res . push_back ( arr [ negMinIdx ] ) ; res . push_back ( arr [ negMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( i != negMinIdx && i != negMaxIdx ) res . push_back ( arr [ i ] ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } else { cout << " No ▁ swap ▁ required STRNEWLINE " ; } } int main ( ) { int arr [ ] = { -4 , 1 , 6 , -3 , -2 , -1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getMinimumSum ( arr , n ) ; return 0 ; }

Index/key variables: i, negMaxIdx, negMinIdx, posMaxIdx, posMinIdx
Accumulator variables: i, stdc
Iterator variables: i