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 = 15Input :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 an average to find the AND value of a sub-array and thus, the over all time complexity will be O(n^{3}).

**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 i^{th} bit set. Let us suppose, there are ‘S_{i}‘ number of sub-arrays with i^{th} bit set. For, i^{th} bit, sum can be updated as sum += (2^{i} * S).

We will break the task to multiple steps. At each step, we will try to find the number of AND values with i^{th} bit set. For this, we will simply iterate through the array and find the number of contiguous segments with i^{th} bit set and there lengths. For, each such segment of length ‘l’, value of sum can be updated as sum += (2^{i} * l * (l + 1))/2.

Since, for each bit, we are performing O(N) iterations, the time complexity of this approach will be O(N).

Below is the implementation of the above idea:

## C++

`// CPP program to find sum of bitwise AND ` `// of all subarrays ` ` ` `#include <iostream> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `// Function to find the sum of ` `// bitwise AND of all subarrays ` `int` `findAndSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// variable to store ` ` ` `// the final sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// multiplier ` ` ` `int` `mul = 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < 30; i++) { ` ` ` `// variable to check if ` ` ` `// counting is on ` ` ` `bool` `count_on = 0; ` ` ` ` ` `// variable to store the ` ` ` `// length of the subarrays ` ` ` `int` `l = 0; ` ` ` ` ` `// loop to find the contiguous ` ` ` `// segments ` ` ` `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; ` ` ` `} ` ` ` ` ` `// updating the multiplier ` ` ` `mul *= 2; ` ` ` `} ` ` ` ` ` `// returning the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 7, 1, 1, 5 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << findAndSum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find Sum of

# bitwise AND of all subarrays

import math as mt

# Function to find the Sum of

# bitwise AND of all subarrays

def findAndSum(arr, n):

# variable to store the final Sum

Sum = 0

# multiplier

mul = 1

for i in range(30):

# variable to check if counting is on

count_on = 0

# variable to store the length

# of the subarrays

l = 0

# loop to find the contiguous

# segments

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

# updating the multiplier

mul *= 2

# returning the Sum

return Sum

# Driver Code

arr = [7, 1, 1, 5]

n = len(arr)

print(findAndSum(arr, n))

# This code is contributed by Mohit Kumar

**Output:**

20

**Time Complexity**: O(N)

## Recommended Posts:

- Sum of bitwise OR of all subarrays
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- Number of subarrays have bitwise OR >= K
- Count distinct Bitwise OR of all Subarrays
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Sum of XOR of all subarrays
- Sum of all Subarrays | Set 1
- Number of subarrays with odd sum
- Sum of minimum elements of all subarrays
- Count subarrays with Prime sum
- Maximize the number of subarrays with XOR as zero
- Queries on XOR of XORs of all subarrays
- Maximum of all Subarrays of size k using set in C++ STL
- Find number of subarrays with even sum