Count of all possible pairs of array elements with same parity | Java program for above approach ; Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver Code
import java . util . * ; class GFG { static 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 ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 1 , 3 } ; int n = A . length ; System . out . println ( countPairs ( A , n ) ) ; } }