Count of all possible pairs of array elements with same parity | C ++ program for the above approach ; Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int A [ ] , int n ) { int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( A [ i ] % 2 == 0 && A [ j ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ j ] % 2 != 0 ) ) count ++ ; } } return count ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << countPairs ( A , n ) ; }