human
Count of even sum triplets in the array for Q range queries | Function to count number of triplets with even sum in range l , r for each query.
Initialization of array.
Initialization of variables.
Traversing array.
If element is odd.
If element is even.
Storing count of even and odd till each i.
Traversing each query.
Count of odd numbers in l to r.
Count of even numbers in l to r.
Finding the ans.
Printing the ans.
Driver Code.
Given Input.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to count of even sum triplets in the array for Q range queries and the other steps required.
 def countTriplets ( size , queries , arr , Q ) :
	arr_even = [ 0 for i in range ( size + 1 ) ]
	arr_odd = [ 0 for i in range ( size + 1 ) ]
	even = 0
	odd = 0
	arr_even [ 0 ] = 0
	arr_odd [ 0 ] = 0
	for i in range ( size ) :
		if ( arr [ i ] % 2 ) :
			odd += 1
		else :
			even += 1
		arr_even [ i + 1 ] = even
		arr_odd [ i + 1 ] = odd
	for i in range ( queries ) :
		l = Q [ i ] [ 0 ]
		r = Q [ i ] [ 1 ]
		odd = arr_odd [ r ] - arr_odd [ l - 1 ]
		even = arr_even [ r ] - arr_even [ l - 1 ]
		ans = ( even * ( even - 1 ) * ( even - 2 ) ) // 6 + ( odd * ( odd - 1 ) // 2 ) * even
		print ( ans , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	N = 6
	Q = 2
	arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
	queries = [ [ 1 , 3 ] , [ 2 , 5 ] ]
	countTriplets ( N , Q , arr , queries )