Print all pairs with given sum | Javascript code to implement the above approach ; Driver code
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 ++ ; } } } var arr = [ 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 ] var n = arr . length ; arr . sort ( function ( a , b ) { return a - b ; } ) ; pairedElements ( arr , 6 , n ) ;