text: Find three element from given three arrays such that their sum is X | Set 2 | Function that returns true if there exists a triplet with sum x ; Sorting arrays such that a [ ] represents smallest array ; Iterating the smallest array ; Two pointers on second and third array ; If a valid triplet is found ; Driver code
code: function existsTriplet ( a , b , c , x , l1 , l2 , l3 ) { if ( l2 <= l1 && l2 <= l3 ) { temp = l1 ; l1 = l2 ; l2 = temp ; temp = a ; a = b ; b = temp ; } else if ( l3 <= l1 && l3 <= l2 ) { temp = l1 ; l1 = l3 ; l3 = temp ; temp = a ; a = c ; c = temp ; } for ( var i = 0 ; i < l1 ; i ++ ) { var j = 0 , k = l3 - 1 ; while ( j < l2 && k >= 0 ) { if ( a [ i ] + b [ j ] + c [ k ] == x ) return true ; if ( a [ i ] + b [ j ] + c [ k ] < x ) j ++ ; else k -- ; } } return false ; } var a = [ 2 , 7 , 8 , 10 , 15 ] ; var b = [ 1 , 6 , 7 , 8 ] ; var c = [ 4 , 5 , 5 ] ; var l1 = a . length ; var l2 = b . length ; var l3 = c . length ; var x = 14 ; if ( existsTriplet ( a , b , c , x , l1 , l2 , l3 ) ) document . write ( " " ) ; else document . write ( " " ) ;