Count of triplets in an Array with odd sum | C ++ Program for the above approach ; 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
#include <iostream> NEW_LINE using namespace std ; int countTriplets ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; else even ++ ; } int c1 = odd * ( even * ( even - 1 ) ) / 2 ; int c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) / 6 ; return c1 + c2 ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int ans = countTriplets ( arr , n ) ; cout << ans ; return 0 ; }