Two Pointers Technique | Two pointer technique based solution to find if there is a pair in A [ 0. . N - 1 ] with a given sum . ; represents first pointer ; represents second pointer ; If we find a pair ; If sum of elements at current pointers is less , we move towards higher values by doing i ++ ; If sum of elements at current pointers is more , we move towards lower values by doing j -- ; array declaration ; value to search ; size of the array ; Function call
function isPairSum ( A , N , X ) { var i = 0 ; var j = N - 1 ; while ( i < j ) { if ( A [ i ] + A [ j ] == X ) return true ; else if ( A [ i ] + A [ j ] < X ) i ++ ; else j -- ; } return false ; } var arr = [ 3 , 5 , 9 , 2 , 8 , 10 , 11 ] ; var val = 17 ; var arrSize = 7 ; document . write ( isPairSum ( arr , arrSize , val ) ) ;