Sum of product of all pairs of a Binary Array | Java program to implement the above approach ; Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; Stores the size of the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code
import java . util . * ; class GFG { static int productSum ( int [ ] arr ) { int cntOne = 0 ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 } ; System . out . println ( productSum ( arr ) ) ; } }