Javascript code to implement the above approach
function pairedElements ( arr , sum , n ) { var low = 0 ; var high = n - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { document . write ( " " + arr [ low ] + " " + arr [ high ] + " " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } }