Sum of product of all pairs of a Binary Array | C ++ 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 ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code ; Stores the size of the given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int productSum ( int arr [ ] , int N ) { int cntOne = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << productSum ( arr , n ) << endl ; }