human
Count number of triplets in an array having sum in the range [ a , b ] | Function to count triplets.
Initialize result.
Fix the first element as A [ i ].
Fix the second element as A [ j ].
Now look for the third number.
Driver code"
Solution in Python:
assistant
#Here is code to count number of triplets in an array having sum in the range [ a , b ] and the other steps required.
 def countTriplets ( arr , n , a , b ) :
	ans = 0
	for i in range ( 0 , n - 2 ) :
		for j in range ( i + 1 , n - 1 ) :
			for k in range ( j + 1 , n ) :
				if ( ( arr [ i ] + arr [ j ] + arr [ k ] >= a ) and ( arr [ i ] + arr [ j ] + arr [ k ] <= b ) ) :
					ans += 1
	return ans
if __name__ == " _ _ main _ _ " :
	arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ]
	n = len ( arr )
	a = 8 ; b = 16
	print ( countTriplets ( arr , n , a , b ) )