Given an array consisting of N positive integers, find the sum of bit-wise and of all possible sub-arrays of the array.
Examples:
Input : arr[] = {1, 5, 8}
Output : 15
Bit-wise AND of {1} = 1
Bit-wise AND of {1, 5} = 1
Bit-wise AND of {1, 5, 8} = 0
Bit-wise AND of {5} = 5
Bit-wise AND of {5, 8} = 0
Bit-wise AND of {8} = 8
Sum = 1 + 1 + 0 + 5 + 0 + 8 = 15
Input : arr[] = {7, 1, 1, 5}
Output : 20
Simple Solution: A simple solution will be to generate all the sub-arrays, and sum up the AND values of all the sub-arrays. It will take linear time on average to find the AND value of a sub-array and thus, the overall time complexity will be O(n3).
Efficient Solution: For the sake of better understanding, let’s assume that any bit of an element is represented by the variable ‘i’, and the variable ‘sum’ is used to store the final sum.
The idea here is, we will try to find the number of AND values(sub-arrays with bit-wise and(&)) with ith bit set. Let us suppose, there is ‘Si‘ number of sub-arrays with ith bit set. For, ith bit, the sum can be updated as sum += (2i * S).
We will break the task into multiple steps. At each step, we will try to find the number of AND values with ith bit set. For this, we will simply iterate through the array and find the number of contiguous segments with ith bit set and their lengths. For, each such segment of length ‘l’, value of sum can be updated as sum += (2i * l * (l + 1))/2.
Since, for each bit, we are performing O(N) iterations and as there are at most log(max(A)) bits, the time complexity of this approach will be O(N*log(max(A)), assuming max(A) = maximum value in the array.
Below is the implementation of the above idea:
C++
#include <iostream>
#include <vector>
using namespace std;
int findAndSum( int arr[], int n)
{
int sum = 0;
int mul = 1;
for ( int i = 0; i < 30; i++) {
bool count_on = 0;
int l = 0;
for ( int j = 0; j < n; j++) {
if ((arr[j] & (1 << i)) > 0)
if (count_on)
l++;
else {
count_on = 1;
l++;
}
else if (count_on) {
sum += ((mul * l * (l + 1)) / 2);
count_on = 0;
l = 0;
}
}
if (count_on) {
sum += ((mul * l * (l + 1)) / 2);
count_on = 0;
l = 0;
}
mul *= 2;
}
return sum;
}
int main()
{
int arr[] = { 7, 1, 1, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findAndSum(arr, n);
return 0;
}
|
Java
class GFG
{
static int findAndSum( int []arr, int n)
{
int sum = 0 ;
int mul = 1 ;
for ( int i = 0 ; i < 30 ; i++)
{
boolean count_on = false ;
int l = 0 ;
for ( int j = 0 ; j < n; j++)
{
if ((arr[j] & ( 1 << i)) > 0 )
if (count_on)
l++;
else
{
count_on = true ;
l++;
}
else if (count_on)
{
sum += ((mul * l * (l + 1 )) / 2 );
count_on = false ;
l = 0 ;
}
}
if (count_on)
{
sum += ((mul * l * (l + 1 )) / 2 );
count_on = false ;
l = 0 ;
}
mul *= 2 ;
}
return sum;
}
public static void main(String[] args)
{
int []arr = { 7 , 1 , 1 , 5 };
int n = arr.length;
System.out.println(findAndSum(arr, n));
}
}
|
Python3
import math as mt
def findAndSum(arr, n):
Sum = 0
mul = 1
for i in range ( 30 ):
count_on = 0
l = 0
for j in range (n):
if ((arr[j] & ( 1 << i)) > 0 ):
if (count_on):
l + = 1
else :
count_on = 1
l + = 1
elif (count_on):
Sum + = ((mul * l * (l + 1 )) / / 2 )
count_on = 0
l = 0
if (count_on):
Sum + = ((mul * l * (l + 1 )) / / 2 )
count_on = 0
l = 0
mul * = 2
return Sum
arr = [ 7 , 1 , 1 , 5 ]
n = len (arr)
print (findAndSum(arr, n))
|
C#
using System;
class GFG
{
static int findAndSum( int []arr, int n)
{
int sum = 0;
int mul = 1;
for ( int i = 0; i < 30; i++)
{
bool count_on = false ;
int l = 0;
for ( int j = 0; j < n; j++)
{
if ((arr[j] & (1 << i)) > 0)
if (count_on)
l++;
else
{
count_on = true ;
l++;
}
else if (count_on)
{
sum += ((mul * l * (l + 1)) / 2);
count_on = false ;
l = 0;
}
}
if (count_on)
{
sum += ((mul * l * (l + 1)) / 2);
count_on = false ;
l = 0;
}
mul *= 2;
}
return sum;
}
public static void Main()
{
int []arr = { 7, 1, 1, 5 };
int n = arr.Length;
Console.Write(findAndSum(arr, n));
}
}
|
PHP
<?php
function findAndSum( $arr , $n )
{
$sum = 0;
$mul = 1;
for ( $i = 0; $i < 30; $i ++)
{
$count_on = 0;
$l = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if (( $arr [ $j ] & (1 << $i )) > 0)
if ( $count_on )
$l ++;
else
{
$count_on = 1;
$l ++;
}
else if ( $count_on )
{
$sum += (( $mul * $l * ( $l + 1)) / 2);
$count_on = 0;
$l = 0;
}
}
if ( $count_on )
{
$sum += (( $mul * $l * ( $l + 1)) / 2);
$count_on = 0;
$l = 0;
}
$mul *= 2;
}
return $sum ;
}
$arr = array ( 7, 1, 1, 5 );
$n = sizeof( $arr );
echo findAndSum( $arr , $n );
?>
|
Javascript
<script>
function findAndSum(arr, n)
{
var sum = 0;
var mul = 1;
for ( var i = 0; i < 30; i++) {
var count_on = 0;
var l = 0;
for ( var j = 0; j < n; j++) {
if ((arr[j] & (1 << i)) > 0)
if (count_on)
l++;
else {
count_on = 1;
l++;
}
else if (count_on) {
sum += ((mul * l * (l + 1)) / 2);
count_on = 0;
l = 0;
}
}
if (count_on) {
sum += ((mul * l * (l + 1)) / 2);
count_on = 0;
l = 0;
}
mul *= 2;
}
return sum;
}
var arr = [ 7, 1, 1, 5 ];
var n = arr.length;
document.write( findAndSum(arr, n));
</script>
|
Time Complexity: O(N*log(max(A))
Auxiliary Space: O(1)