Now start two index variables from two corners of array and move them toward each other .
i = 0 ; j = size - 1 ; while ( i < size && j >= 0 ) { if ( ( aux [ i ] . sum + aux [ j ] . sum == X ) && noCommon ( aux [ i ] , aux [ j ] ) ) { cout << arr [ aux [ i ] . first ] << " , ▁ " << arr [ aux [ i ] . sec ] << " , ▁ " << arr [ aux [ j ] . first ] << " , ▁ " << arr [ aux [ j ] . sec ] << endl ; return ; } else if ( aux [ i ] . sum + aux [ j ] . sum < X ) i ++ ; else j -- ; } }