Count of triplets in an Array with odd sum | Function to count the number of unordered triplets such that their sum is an odd integer ; Count the number of odd and even integers in the array ; Number of ways to create triplets using one odd and two even integers ; Number of ways to create triplets using three odd integers ; Return answer ; Driver Code ; Function Call ; Print Answer
def countTriplets ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT c1 = odd * ( even * ( even - 1 ) ) // 2 NEW_LINE c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) // 6 NEW_LINE return c1 + c2 NEW_LINE DEDENT arr = [ 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE ans = countTriplets ( arr , n ) NEW_LINE print ( ans ) NEW_LINE