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
def countTriplets ( size , queries , arr , Q ) : NEW_LINE INDENT arr_even = [ 0 for i in range ( size + 1 ) ] NEW_LINE arr_odd = [ 0 for i in range ( size + 1 ) ] NEW_LINE even = 0 NEW_LINE odd = 0 NEW_LINE arr_even [ 0 ] = 0 NEW_LINE arr_odd [ 0 ] = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( arr [ i ] % 2 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT arr_even [ i + 1 ] = even NEW_LINE arr_odd [ i + 1 ] = odd NEW_LINE DEDENT for i in range ( queries ) : NEW_LINE INDENT l = Q [ i ] [ 0 ] NEW_LINE r = Q [ i ] [ 1 ] NEW_LINE odd = arr_odd [ r ] - arr_odd [ l - 1 ] NEW_LINE even = arr_even [ r ] - arr_even [ l - 1 ] NEW_LINE ans = ( even * ( even - 1 ) * ( even - 2 ) ) // 6 + ( odd * ( odd - 1 ) // 2 ) * even NEW_LINE print ( ans , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE Q = 2 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE queries = [ [ 1 , 3 ] , [ 2 , 5 ] ] NEW_LINE countTriplets ( N , Q , arr , queries ) NEW_LINE DEDENT