Count Pairs from two arrays with even sum | Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a ; Find the count of odd and even elements in b ; Count the number of pairs ; Return the number of pairs ; Driver code
function count_pairs ( a , b , n , m ) { var odd1 = 0 , even1 = 0 ; var odd2 = 0 , even2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } var pairs = Math . min ( odd1 , odd2 ) + Math . min ( even1 , even2 ) ; return pairs ; } var a = [ 9 , 14 , 6 , 2 , 11 ] ; var b = [ 8 , 4 , 7 , 20 ] ; var n = a . length ; var m = b . length ; document . write ( count_pairs ( a , b , n , m ) ) ;