human
Write me a code to print triplets with sum less than k and the other steps required given the following:
Print triplets with sum less than k | Python3 program to print triplets with sum smaller than a given value.
Sort input array.
Every iteration of loop counts triplet with first element as arr [ i ] ..
Initialize other two elements as corner elements of subarray arr [ j + 1. . k ].
Use Meet in the Middle concept.
If sum of current triplet is more or equal , move right corner to look for smaller values.
Else move left corner.
This is important . For current i and j , there are total k - j third elements ..
Driver code"
How can the above be solved in Python?
assistant
#Here is code to print triplets with sum less than k and the other steps required.
 def printTriplets ( arr , n , sum ) :
	arr . sort ( )
	for i in range ( n - 2 ) :
		( j , k ) = ( i + 1 , n - 1 )
		while ( j < k ) :
			if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) :
				k -= 1
			else :
				for x in range ( j + 1 , k + 1 ) :
					print ( str ( arr [ i ] ) + " , ▁ " + str ( arr [ j ] ) + " , ▁ " + str ( arr [ x ] ) )
				j += 1
if __name__ == " _ _ main _ _ " :
	arr = [ 5 , 1 , 3 , 4 , 7 ]
	n = len ( arr )
	sum = 12
	printTriplets ( arr , n , sum ) ;