Store sums of all pairs in a hash table
Dictionary < int , pair > mp = new Dictionary < int , pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( mp . ContainsKey ( arr [ i ] + arr [ j ] ) ) mp [ arr [ i ] + arr [ j ] ] = new pair ( i , j ) ; else mp . Add ( arr [ i ] + arr [ j ] , new pair ( i , j ) ) ;