human
Count triplets with sum smaller than a given value | Python3 program to count triplets with sum smaller than a given value.
Sort input array.
Initialize result.
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 can be total k - j third elements ..
Driver program"
Solution in Python:
assistant
#Here is code to count triplets with sum smaller than a given value and the other steps required.
 def countTriplets ( arr , n , sum ) :
	arr . sort ( )
	ans = 0
	for i in range ( 0 , n - 2 ) :
		j = i + 1
		k = n - 1
		while ( j < k ) :
			if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) :
				k = k - 1
			else :
				ans += ( k - j )
				j = j + 1
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ 5 , 1 , 3 , 4 , 7 ]
	n = len ( arr )
	sum = 12
	print ( countTriplets ( arr , n , sum ) )