📜  二进制数组所有对的乘积总和

📅  最后修改于: 2021-09-07 05:23:42             🧑  作者: Mango

给定一个大小为N的二进制数组arr[] ,任务是打印所有给定数组元素对的乘积之和。

注意:二进制数组只包含 0 和 1。

例子:

朴素方法:解决问题的最简单方法是使用从数组中生成所有可能的对并计算它们的乘积之和。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了优化上述方法,只考虑两个元素都是1 的对。以下是观察结果:

请按照以下步骤解决问题:

  • 初始化变量cntOne以存储给定数组中1的计数。
  • 最后,返回cntOne * (cntOne – 1) / 2 的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
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


输出:
3

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live