Count of all possible pairs of array elements with same parity | Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver code
function countPairs ( A , n ) { let 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 ; } let A = [ 1 , 2 , 3 , 1 , 3 ] ; let n = A . length ; document . write ( countPairs ( A , n ) ) ;