Sum of Bitwise AND of each array element with the elements of another array
Last Updated :
07 Sep, 2021
Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise AND of each element of arr1[] with the elements of the array arr2[].
Examples:
Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3
Output: 2 4 6
Explanation:
For elements at index 0 in arr1[], Sum = arr1[0] & arr2[0] + arr1[0] & arr2[1] + arr1[0] & arr2[2], Sum = 1 & 1 + 1 & 2 + 1 & 3 = 2
For elements at index 1 in arr1[], Sum = arr1[1] & arr2[0] + arr1[1] & arr2[1] + arr1[1] & arr2[2], Sum= 2 & 1 + 2 & 2 + 2 & 3 = 4
For elements at index 2 in arr1[], Sum = arr1[2] & arr2[0] + arr1[2] & arr2[1] + arr1[2] & arr2[2], Sum= 3 & 1 + 3 & 2 + 3 & 3 = 6
Input: arr1[] = {2, 4, 8, 16}, arr2[] = {2, 4, 8, 16}, M = 4, N = 4
Output: 2 4 8 16
Naive Approach: The simplest approach to solve the problem is to traverse the array arr1[] and for each element of the array arr1[], traverse the array arr2[] and calculate the sum of Bitwise AND of the current elements of arr1[] with all elements of arr2[] for each element
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use bit manipulation to solve the above problem. Suppose every element of the array can be represented using 32 bits only.
- According to the bitwise AND property, while performing the operation, the ith bit will be set bit only when both numbers have a set bit at the ith position, where 0?i<32.
- Therefore, for a number in arr1[], If the ith bit is a set bit, then the ith place will contribute a sum of K*2i, where K is the total number of numbers in arr2[] having set the bit at the ith position.
Follow the steps below to solve the problem:
- Initialize an integer array frequency[] to store the count of numbers in arr2[] having set the bit at ith position where 0?i<32
- Traverse in the array arr2[] and for each element represent it in binary form and increment the count in the frequency[] array by one at the position having 1 in the binary representation.
- Traverse the array arr1[]
- Initialize an integer variable bitwise_AND_sum with 0.
- Traverse in the range [0, 31] using a variable j.
- If the jth bit is set to bit in the binary representation of arr2[i] then increment bitwise_AND_sum by frequency[j]*2j.
- Print the sum obtained i.e., bitwise_AND_sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Bitwise_AND_sum_i( int arr1[], int arr2[], int M, int N)
{
int frequency[32] = { 0 };
for ( int i = 0; i < N; i++) {
int bit_position = 0;
int num = arr1[i];
while (num) {
if (num & 1) {
frequency[bit_position] += 1;
}
bit_position += 1;
num >>= 1;
}
}
for ( int i = 0; i < M; i++) {
int num = arr2[i];
int value_at_that_bit = 1;
int bitwise_AND_sum = 0;
for ( int bit_position = 0; bit_position < 32;
bit_position++) {
if (num & 1) {
bitwise_AND_sum += frequency[bit_position]
* value_at_that_bit;
}
num >>= 1;
value_at_that_bit <<= 1;
}
cout << bitwise_AND_sum << ' ' ;
}
return ;
}
int main()
{
int arr1[] = { 1, 2, 3 };
int arr2[] = { 1, 2, 3 };
int N = sizeof (arr1) / sizeof (arr1[0]);
int M = sizeof (arr2) / sizeof (arr2[0]);
Bitwise_AND_sum_i(arr1, arr2, M, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void main(String[] args)
{
int [] arr1 = { 1 , 2 , 3 };
int [] arr2 = { 1 , 2 , 3 };
int N = arr1.length;
int M = arr2.length;
Bitwise_AND_sum_i(arr1, arr2, M, N);
}
static void Bitwise_AND_sum_i( int arr1[], int arr2[],
int M, int N)
{
int [] frequency = new int [ 32 ];
for ( int i = 0 ; i < N; i++)
{
int bit_position = 0 ;
int num = arr1[i];
while (num != 0 )
{
if ((num & 1 ) != 0 )
{
frequency[bit_position] += 1 ;
}
bit_position += 1 ;
num >>= 1 ;
}
}
for ( int i = 0 ; i < M; i++)
{
int num = arr2[i];
int value_at_that_bit = 1 ;
int bitwise_AND_sum = 0 ;
for ( int bit_position = 0 ; bit_position < 32 ;
bit_position++)
{
if ((num & 1 ) != 0 )
{
bitwise_AND_sum
+= frequency[bit_position]
* value_at_that_bit;
}
num >>= 1 ;
value_at_that_bit <<= 1 ;
}
System.out.print( bitwise_AND_sum + " " );
}
}
}
|
Python3
def Bitwise_AND_sum_i(arr1, arr2, M, N):
frequency = [ 0 ] * 32
for i in range (N):
bit_position = 0
num = arr1[i]
while (num):
if (num & 1 ):
frequency[bit_position] + = 1
bit_position + = 1
num >> = 1
for i in range (M):
num = arr2[i]
value_at_that_bit = 1
bitwise_AND_sum = 0
for bit_position in range ( 32 ):
if (num & 1 ):
bitwise_AND_sum + = frequency[bit_position] * value_at_that_bit
num >> = 1
value_at_that_bit << = 1
print (bitwise_AND_sum, end = " " )
return
if __name__ = = '__main__' :
arr1 = [ 1 , 2 , 3 ]
arr2 = [ 1 , 2 , 3 ]
N = len (arr1)
M = len (arr2)
Bitwise_AND_sum_i(arr1, arr2, M, N)
|
C#
using System;
class GFG
{
static public void Main()
{
int [] arr1 = { 1, 2, 3 };
int [] arr2 = { 1, 2, 3 };
int N = arr1.Length;
int M = arr2.Length;
Bitwise_AND_sum_i(arr1, arr2, M, N);
}
static void Bitwise_AND_sum_i( int [] arr1, int [] arr2,
int M, int N)
{
int [] frequency = new int [32];
for ( int i = 0; i < N; i++)
{
int bit_position = 0;
int num = arr1[i];
while (num != 0)
{
if ((num & 1) != 0)
{
frequency[bit_position] += 1;
}
bit_position += 1;
num >>= 1;
}
}
for ( int i = 0; i < M; i++)
{
int num = arr2[i];
int value_at_that_bit = 1;
int bitwise_AND_sum = 0;
for ( int bit_position = 0; bit_position < 32;
bit_position++) {
if ((num & 1) != 0)
{
bitwise_AND_sum
+= frequency[bit_position]
* value_at_that_bit;
}
num >>= 1;
value_at_that_bit <<= 1;
}
Console.Write(bitwise_AND_sum + " " );
}
}
}
|
Javascript
<script>
function Bitwise_AND_sum_i(arr1, arr2, M, N) {
let frequency = new Array(32).fill(0);
for (let i = 0; i < N; i++) {
let bit_position = 0;
let num = arr1[i];
while (num) {
if (num & 1) {
frequency[bit_position] += 1;
}
bit_position += 1;
num >>= 1;
}
}
for (let i = 0; i < M; i++) {
let num = arr2[i];
let value_at_that_bit = 1;
let bitwise_AND_sum = 0;
for (let bit_position = 0; bit_position < 32; bit_position++) {
if (num & 1) {
bitwise_AND_sum += frequency[bit_position] * value_at_that_bit;
}
num >>= 1;
value_at_that_bit <<= 1;
}
document.write(bitwise_AND_sum + ' ' );
}
return ;
}
let arr1 = [1, 2, 3];
let arr2 = [1, 2, 3];
let N = arr1.length;
let M = arr2.length
Bitwise_AND_sum_i(arr1, arr2, M, N);
</script>
|
Time Complexity: O(N * 32)
Auxiliary Space: O(N * 32)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...