Related Articles

# Sum of Bitwise AND of all pairs possible from two arrays

• Last Updated : 07 Apr, 2021

Given two arrays A[] and B[] of size N and M respectively, the task is to find the sum of Bitwise AND of all possible unordered pairs (A[i], B[j]) from the two arrays.

Examples:

Input: A[] = {1, 2} , B[] = {3, 4}
Output:
Explanation:
Bitwise AND of all possible pairs are
1 & 3  = 1
1 & 4 = 0
2 & 3 = 2
2 & 4 = 0
Therefore, the sum of bitwise AND of all possible pairs are = (1 + 0 + 2 + 0) = 3

Input: A[] = {4, 6, 0, 0, 3, 3}, B[] = {0, 5, 6, 5, 0, 3}
Output: 42

Approach: To solve the problem, the idea is to traverse both the arrays and generate all possible pairs from the given two arrays and keep adding their respective Bitwise ANDs. Finally, print the sum of Bitwise AND of all possible pairs (A[i], B[j]) obtained from the two given arrays.

Follow the steps below to solve the problem:

• Initialize a variable, say pairsAndSum to store the sum of Bitwise AND of all possible pairs.
• Traverse both the array and generate all possible pairs from the given two arrays.
• Finally, calculate the sum of Bitwise AND of all possible pairs from both the arrays and print the sum.

Below is the implementation of the above approach:

## 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);` `    ``int` `M = ``sizeof``(B) / ``sizeof``(B);``    ` `    ``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

 ``
Output:
`42`

Time Complexity: O(N2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up