human
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"
How can the above be solved in Python?
assistant
#Here is code to find three element from given three arrays such that their sum is X and the other steps required.
 def existsTriplet ( a , b , c , x , l1 , l2 , l3 ) :
	if ( l2 <= l1 and l2 <= l3 ) :
		l1 , l2 = l2 , l1
		a , b = b , a
	elif ( l3 <= l1 and l3 <= l2 ) :
		l1 , l3 = l3 , l1
		a , c = c , a
	for i in range ( l1 ) :
		j = 0
		k = l3 - 1
		while ( j < l2 and k >= 0 ) :
			if ( a [ i ] + b [ j ] + c [ k ] == x ) :
				return True
			if ( a [ i ] + b [ j ] + c [ k ] < x ) :
				j += 1
			else :
				k -= 1
	return False
a = [ 2 , 7 , 8 , 10 , 15 ]
b = [ 1 , 6 , 7 , 8 ]
c = [ 4 , 5 , 5 ]
l1 = len ( a )
l2 = len ( b )
l3 = len ( c )
x = 14
if ( existsTriplet ( a , b , c , x , l1 , l2 , l3 ) ) :
	print ( " Yes " )
else :
	print ( " No " )