Search if a pair can be formed with arr [ i ] .
var rem = sum - arr [ i ] ; if ( m . hasOwnProperty ( rem ) ) { var count = m [ rem ] ; for ( var j = 0 ; j < count ; j ++ ) { document . write ( " " + rem + " " + arr [ i ] + " " + " " ) ; } } if ( m . hasOwnProperty ( arr [ i ] ) ) { m [ arr [ i ] ] ++ ; } else { m [ arr [ i ] ] = 1 ; } } }