📌  相关文章
📜  两个数组中所有可能对的按位与的总和

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

给定两个大小分别为NM 的数组A[]B[] ,任务是从两个数组中找到所有可能的无序对(A[i], B[j])的按位与的和。

例子:

方法:为了解决这个问题,我们的想法是遍历两个数组并从给定的两个数组中生成所有可能的对,并继续添加它们各自的Bitwise AND 。最后,打印从两个给定数组获得的所有可能对(A[i], B[j])的按位与的总和。

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

  • 初始化一个变量,比如pairsAndSum来存储所有可能对的按位与的总和。
  • 遍历两个数组并从给定的两个数组中生成所有可能的对。
  • 最后,计算两个数组中所有可能对的按位与的总和并打印总和。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the sum of
// AND of all possible pair
int sumOfAnd(int A[], int B[],
                int N, int M)
{
 
    // Stores sum of bitwise AND
    // of  all possible pair
    int pairsAndSum = 0;
 
    // Traverse the array A[]
    for (int i = 0; i < N; i++) {
         
       // Traverse the array B[]
        for (int j = 0; j < M;
                           j++) {
 
            // Update pairsAndSum
            pairsAndSum +=
                   (A[i] & B[j]);
        }
    }
     
    return pairsAndSum;
}
 
// Driver Code
int main()
{
 
    int A[] = { 4, 6, 0, 0, 3, 3 };
    int B[] = { 0, 5, 6, 5, 0, 3 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    int M = sizeof(B) / sizeof(B[0]);
     
    cout << sumOfAnd(A, B, N, M);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the sum of
// AND of all possible pair
static int sumOfAnd(int A[], int B[],
                    int N, int M)
{
  // Stores sum of bitwise AND
  // of  all possible pair
  int pairsAndSum = 0;
 
  // Traverse the array A[]
  for (int i = 0; i < N; i++)
  {
    // Traverse the array B[]
    for (int j = 0; j < M; j++)
    {
      // Update pairsAndSum
      pairsAndSum += (A[i] & B[j]);
    }
  }
 
  return pairsAndSum;
}
 
// Driver Code
public static void main(String[] args)
{
  int A[] = {4, 6, 0, 0, 3, 3};
  int B[] = {0, 5, 6, 5, 0, 3};
  int N = A.length;
  int M = B.length;
  System.out.print(sumOfAnd(A, B,
                            N, M));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above approach
 
# Function to find the sum of
# AND of all possible pair
def sumOfAnd(A, B, N, M):
     
    # Stores sum of bitwise AND
    # of all possible pair
    pairsAndSum = 0
 
    # Traverse the array A
    for i in range(N):
         
        # Traverse the array B
        for j in range(M):
             
            # Update pairsAndSum
            pairsAndSum += (A[i] & B[j])
 
    return pairsAndSum
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 4, 6, 0, 0, 3, 3 ]
    B = [ 0, 5, 6, 5, 0, 3 ]
     
    N = len(A)
    M = len(B)
     
    print(sumOfAnd(A, B, N, M))
 
# This code is contributed by Amit Katiyar


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to find the sum of
// AND of all possible pair
static int sumOfAnd(int []A, int []B,
                    int N, int M)
{
  // Stores sum of bitwise AND
  // of  all possible pair
  int pairsAndSum = 0;
 
  // Traverse the array []A
  for (int i = 0; i < N; i++)
  {
    // Traverse the array []B
    for (int j = 0; j < M; j++)
    {
      // Update pairsAndSum
      pairsAndSum += (A[i] & B[j]);
    }
  }
 
  return pairsAndSum;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []A = {4, 6, 0, 0, 3, 3};
  int []B = {0, 5, 6, 5, 0, 3};
  int N = A.Length;
  int M = B.Length;
  Console.Write(sumOfAnd(A, B,
                            N, M));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
42

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

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