Count number of ordered pairs with Even and Odd Sums | C ++ implementation of the above approach ; function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_odd_pair ( int n , int a [ ] ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } int ans = odd * even * 2 ; return ans ; } int count_even_pair ( int odd_sum_pairs , int n ) { int total_pairs = ( n * ( n - 1 ) ) ; int ans = total_pairs - odd_sum_pairs ; return ans ; } int main ( ) { int n = 6 ; int a [ ] = { 2 , 4 , 5 , 9 , 1 , 8 } ; int odd_sum_pairs = count_odd_pair ( n , a ) ; int even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) ; cout << " Even ▁ Sum ▁ Pairs ▁ = ▁ " << even_sum_pairs << endl ; cout << " Odd ▁ Sum ▁ Pairs = ▁ " << odd_sum_pairs << endl ; return 0 ; }