Sum of product of all pairs of a Binary Array
Last Updated :
01 Jun, 2021
Given a binary array arr[] of size N, the task is to print the sum of product of all pairs of the given array elements.
Note: The binary array contains only 0 and 1.
Examples:
Input: arr[] = {0, 1, 1, 0, 1}
Output: 3
Explanation: Sum of product of all possible pairs are: {0 × 1 + 0 × 1 + 0 × 0 + 0 × 1 + 1 × 1 + 1 × 0 + 1 × 1 + 1 × 0 + 1 × 1 + 0 × 1}.
Therefore, the required output is 3.
Input: arr[] = {1, 1, 1, 1}
Output: 6
Naive Approach: The simplest approach to solve the problem is to use generate all possible pairs from the array and calculate the sum of their product.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, consider only those pairs in which both the elements are 1. Following are the observations:
If there is a pair (arr[i], arr[j]) where arr[i] × arr[j] = 1, then arr[i] and arr[j] must be 1.
Total number of pairs that satisfy (arr[i] × arr[j] = 1) are:
=>
\binom{cntOne}{2}
=> cntOne × (cntOne - 1) / 2
where, cntOne is the count of 1s in the given array
Follow the steps below to solve the problem:
- Initialize the variable cntOne to store the count of 1s from the given array.
- Finally, return the value of cntOne * (cntOne - 1) / 2.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the sum of product
// of all pairs of the given array
int productSum(int arr[], int N)
{
// Stores count of one in
// the given array
int cntOne = 0;
for(int i = 0; i < N; i++)
{
// If current element is 1
if (arr[i] == 1)
// Increase count
cntOne++;
}
// Return the sum of product
// of all pairs
return cntOne * (cntOne - 1) / 2;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 1, 0, 1 };
// Stores the size of
// the given array
int n = sizeof(arr) / sizeof(arr[0]);
cout << productSum(arr, n) << endl;
}
// This code is contributed by code_hunt
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
// Function to print the sum of product
// of all pairs of the given array
static int productSum(int[] arr)
{
// Stores count of one in
// the given array
int cntOne = 0;
// Stores the size of
// the given array
int N = arr.length;
for (int i = 0; i < N; i++) {
// If current element is 1
if (arr[i] == 1)
// Increase count
cntOne++;
}
// Return the sum of product
// of all pairs
return cntOne * (cntOne - 1) / 2;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 0, 1, 1, 0, 1 };
System.out.println(productSum(arr));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to print the sum of product
# of all pairs of the given array
def productSum(arr):
# Stores count of one in
# the given array
cntOne = 0
# Stores the size of
# the given array
N = len(arr)
for i in range(N):
# If current element is 1
if (arr[i] == 1):
# Increase count
cntOne += 1
# Return the sum of product
# of all pairs
return cntOne * (cntOne - 1) // 2
# Driver Code
arr = [ 0, 1, 1, 0, 1 ]
print(productSum(arr))
# This code is contributed by code_hunt
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the sum of product
// of all pairs of the given array
static int productSum(int[] arr)
{
// Stores count of one in
// the given array
int cntOne = 0;
// Stores the size of
// the given array
int N = arr.Length;
for(int i = 0; i < N; i++)
{
// If current element is 1
if (arr[i] == 1)
// Increase count
cntOne++;
}
// Return the sum of product
// of all pairs
return cntOne * (cntOne - 1) / 2;
}
// Driver Code
public static void Main()
{
int[] arr = { 0, 1, 1, 0, 1 };
Console.Write(productSum(arr));
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to print the sum of product
// of all pairs of the given array
function productSum(arr, N)
{
// Stores count of one in
// the given array
let cntOne = 0;
for(let i = 0; i < N; i++)
{
// If current element is 1
if (arr[i] == 1)
// Increase count
cntOne++;
}
// Return the sum of product
// of all pairs
return cntOne * (cntOne - 1) / 2;
}
// Driver Code
let arr = [ 0, 1, 1, 0, 1 ];
// Stores the size of
// the given array
let n = arr.length;
document.write(productSum(arr, n) + "<br>");
// This code is contributed by Mayank Tyagi
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Sum of product of all pairs of array elements Given an array A[] of integers find sum of product of all pairs of array elements i. e., we need to find of product after execution of following pseudo code product = 0 for i = 1:n for j = i+1:n product = product + A[i]*A[j] Examples: Input : A[] = {1, 3, 4} Output : 19 Possible Pairs : (1,3), (1,4)
12 min read
Sum of all ordered pair-products from a given array Given an array arr[] of size N, the task is to find the sum of all products of ordered pairs that can be generated from the given array elements.Examples: Input: arr[] ={1, 2, 3}Output: 36Explanation:All possible pairs are {(1, 1), {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}. The
4 min read
Sum of Bitwise OR of all pairs in a given array Given an array "arr[0..n-1]" of integers. The task is to calculate the sum of Bitwise OR of all pairs, i.e. calculate the sum of "arr[i] | arr[j]" for all the pairs in the given array where i < j. Here '|' is a bitwise OR operator. The expected time complexity is O(n). Examples: Input: arr[] = {5
13 min read
Sum of XOR of all pairs in an array Given an array of n integers, find the sum of xor of all pairs of numbers in the array. Examples : Input : arr[] = {7, 3, 5}Output : 127 ^ 3 = 43 ^ 5 = 67 ^ 5 = 2Sum = 4 + 6 + 2 = 12Input : arr[] = {5, 9, 7, 6}Output : 475 ^ 9 = 129 ^ 7 = 147 ^ 6 = 15 ^ 7 = 25 ^ 6 = 39 ^ 6 = 15Sum = 12 + 14 + 1 + 2
11 min read
Sum of Bitwise And of all pairs in a given array Given an array "arr[0..n-1]" of integers, calculate sum of "arr[i] & arr[j]" for all the pairs in the given where i < j. Here & is bitwise AND operator. Expected time complexity is O(n). Examples : Input: arr[] = {5, 10, 15} Output: 15 Required Value = (5 & 10) + (5 & 15) + (10
13 min read