Count pairs in an array containing at least one even value | Java implementation to Count pairs in an array such that each pair contains at least one even element ; Function to count the pairs in the array such as there is at least one even element in each pair ; store count of even and odd elements ; check element is even or odd ; Driver Code
import java . util . * ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return ( even * ( even - 1 ) ) / 2 + ( even * odd ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 2 , 3 , 1 , 4 , 2 } ; int n = arr . length ; System . out . println ( CountPairs ( arr , n ) ) ; } }