Sum of Bitwise And of all pairs in a given array
Last Updated :
13 Oct, 2022
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 & 15)
= 0 + 5 + 10
= 15
Input: arr[] = {1, 2, 3, 4}
Output: 3
Required Value = (1 & 2) + (1 & 3) + (1 & 4) +
(2 & 3) + (2 & 4) + (3 & 4)
= 0 + 1 + 0 + 2 + 0 + 0
= 3
A Brute Force approach is to run two loops and time complexity is O(n2).
C++
#include <bits/stdc++.h>
using namespace std;
int pairAndSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
ans += arr[i] & arr[j];
return ans;
}
int main()
{
int arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << pairAndSum(arr, n) << endl;
return 0;
}
|
C
#include <stdio.h>
int pairAndSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
ans += arr[i] & arr[j];
return ans;
}
int main()
{
int arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d\n" ,pairAndSum(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int pairAndSum( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
ans += arr[i] & arr[j];
return ans;
}
public static void main(String args[])
{
int arr[] = { 5 , 10 , 15 };
int n = arr.length;
System.out.println(pairAndSum(arr, n));
}
}
|
Python3
def pairAndSum(arr, n) :
ans = 0
for i in range ( 0 ,n) :
for j in range ((i + 1 ),n) :
ans = ans + arr[i] & arr[j]
return ans
arr = [ 5 , 10 , 15 ]
n = len (arr)
print (pairAndSum(arr, n))
|
C#
using System;
class GFG {
static int pairAndSum( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i+1; j < n; j++)
ans += arr[i] & arr[j];
return ans;
}
public static void Main()
{
int []arr = {5, 10, 15};
int n = arr.Length;
Console.Write(pairAndSum(arr, n) );
}
}
|
PHP
<?php
function pairAndSum( $arr , $n )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
$ans += $arr [ $i ] & $arr [ $j ];
return $ans ;
}
$arr = array (5, 10, 15);
$n = sizeof( $arr ) ;
echo pairAndSum( $arr , $n ), "\n" ;
?>
|
Javascript
<script>
function pairAndSum(arr, n)
{
let ans = 0;
for (let i = 0; i < n; i++)
for (let j = i+1; j < n; j++)
ans += arr[i] & arr[j];
return ans;
}
let arr = [5, 10, 15];
let n = arr.length;
document.write(pairAndSum(arr, n));
</script>
|
Output :
15
Time Complexity: O(n2)
Auxiliary Space: O(1)
An Efficient Solution can solve this problem in O(n) time. The assumption here is that integers are represented using 32 bits.
The idea is to count number of set bits at every i’th position (i>=0 && i<=31). Any i’th bit of the AND of two numbers is 1 if the corresponding bit in both the numbers is equal to 1.
Let k be the count of set bits at i’th position. Total number of pairs with i’th set bit would be kC2 = k*(k-1)/2 (Count k means there are k numbers which have i’th set bit). Every such pair adds 2i to total sum. Similarly, we work for all other places and add the sum to our final answer.
This idea is similar to this. Below is the implementation.
C++
#include <bits/stdc++.h>
using namespace std;
int pairAndSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < 32; i++) {
int k = 0;
for ( int j = 0; j < n; j++)
if ((arr[j] & (1 << i)))
k++;
ans += (1 << i) * (k * (k - 1) / 2);
}
return ans;
}
int main()
{
int arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << pairAndSum(arr, n) << endl;
return 0;
}
|
C
#include <stdio.h>
int pairAndSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < 32; i++) {
int k = 0;
for ( int j = 0; j < n; j++)
if ((arr[j] & (1 << i)))
k++;
ans += (1 << i) * (k * (k - 1) / 2);
}
return ans;
}
int main()
{
int arr[] = { 5, 10, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d\n" ,pairAndSum(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int pairAndSum( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < 32 ; i++) {
int k = 0 ;
for ( int j = 0 ; j < n; j++) {
if ((arr[j] & ( 1 << i)) != 0 )
k++;
}
ans += ( 1 << i) * (k * (k - 1 ) / 2 );
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 5 , 10 , 15 };
int n = arr.length;
System.out.println(pairAndSum(arr, n));
}
}
#include <stdio.h>
int pairAndSum( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < 32 ; i++) {
int k = 0 ;
for ( int j = 0 ; j < n; j++)
if ((arr[j] & ( 1 << i)))
k++;
ans += ( 1 << i) * (k * (k - 1 ) / 2 );
}
return ans;
}
int main()
{
int arr[] = { 5 , 10 , 15 };
int n = sizeof(arr) / sizeof(arr[ 0 ]);
printf( "%d\n" ,pairAndSum(arr, n));
return 0 ;
}
|
Python3
def pairAndSum(arr, n) :
ans = 0
for i in range ( 0 , 32 ) :
k = 0
for j in range ( 0 ,n) :
if ( (arr[j] & ( 1 << i)) ) :
k = k + 1
ans = ans + ( 1 << i) * (k * (k - 1 ) / / 2 )
return ans
arr = [ 5 , 10 , 15 ]
n = len (arr)
print (pairAndSum(arr, n))
|
C#
using System;
class GFG {
static int pairAndSum( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < 32; i++)
{
int k = 0;
for ( int j = 0; j < n; j++)
{
if ((arr[j] & (1 << i))!=0)
k++;
}
ans += (1 << i) * (k * (k - 1)/2);
}
return ans;
}
public static void Main()
{
int []arr = new int []{5, 10, 15};
int n = arr.Length;
Console.Write(pairAndSum(arr, n));
}
}
|
PHP
<?php
function pairAndSum( $arr , $n )
{
$ans = 0;
for ( $i = 0; $i < 32; $i ++)
{
$k = 0;
for ( $j = 0; $j < $n ; $j ++)
if (( $arr [ $j ] & (1 << $i )) )
$k ++;
$ans += (1 << $i ) * ( $k * ( $k - 1) / 2);
}
return $ans ;
}
$arr = array (5, 10, 15);
$n = sizeof( $arr );
echo pairAndSum( $arr , $n ) ;
?>
|
Javascript
<script>
function pairAndSum(arr, n)
{
let ans = 0;
for (let i = 0; i < 32; i++)
{
let k = 0;
for (let j = 0; j < n; j++)
{
if ((arr[j] & (1 << i))!=0)
k++;
}
ans += (1 << i) * (k * (k - 1)/2);
}
return ans;
}
let arr = [5, 10, 15];
let n = arr.length;
document.write(pairAndSum(arr, n));
</script>
|
Output:
15
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...