Skip to content
Related Articles

Related Articles

Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array
  • Last Updated : 25 Feb, 2021

Given an array arr[] consisting of N integers, the task is to find the sum of Bitwise AND of (arr[i] + arr[j]) and Bitwise AND of arr[i] and arr[j] for each pair of elements (arr[i], arr[j]) from the given array. Since the sum can be very large, print it modulo (109 + 7).

Examples:

Input: arr[] = {8, 9}
Output: 0
Explanation: The only pair from the array is (8, 9). Sum of the pair = (8 + 9) = 17. Bitwise AND of the pairs = (8 & 9) = 8. Therefore required Bitwise AND = (17 & 8) = 0.

Input: arr[] = {1, 3, 3}
Output: 2
Explanation:
Pair (1, 3): Required Bitwise AND = (1 + 3) & (1 & 3) = (4 & 1) = 0.
Pair (3, 3): Required Bitwise AND = (3 + 3) & (3 & 3) = (6 & 3) = 2.
Therefore, total sum = 0 + 0 + 2 = 2.

Naive Approach: The simplest approach is to generate all possible pairs from the given array and check if there exist any such pair that satisfy the given condition or not. If found to be true, then count this pair. After checking for all the pairs, print the resultant count
Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:



  • Considering the pair (X, Y), the Bitwise AND at a bit, say bth is 1, then (X + Y) must also have the bth bit as a set bit to contribute at the bth bit.
  • Considering only the last b bits of both the numbers of such pairs, it can be observed that to satisfy the above condition, bth bit and (b + 1)th bit, both will have to be set.
  • Therefore, from the above steps, it can be derived that to satisfy the above conditions, the following condition should satisfy:

X + Y - 2^{b+1} - 2^{b} >= 0 \newline Y >= 3 * 2^{b} - X

  • Therefore, the task reduces to find the count of pairs satisfying the above condition for each bit position and increment the result by the value cnt*2b  for 0 ≤ b < 31.

Follow the steps below to solve the problem:

  • Initialize a variable, say ans, to store the resultant sum.
  • Iterate over the range [0, 30] and perform the following operations:
    • Initialize a vector V that stores the pairs satisfying the above conditions.
    • Traverse the given array arr[] and insert the value arr[j] – (arr[j] >> (i + 1))*(1 << (i + 1)) modulo M in the vector V, if ((arr[j] >> (i + 1)) &1) is true.
    • Sort the vector V in ascending order.
    • Traverse the vector V and perform the following:
      • Calculate the value 2(i + 1) + 2i – V[j] and store it in a variable say Y.
      • Find count of points in V which is greater than or equal to Y in V and store it in a variable say cnt i.e., cnt = V.size() – (lower_bound(V.begin() + j + 1, V.end(), Y) – V.begin()).
      • Update the ans as ans  = (ans+ cnt* 2i)%M.
  • After completing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

C++14




// C++ program for the above approach
 
#include <bits/stdc++.h>
#define M 1000000007
using namespace std;
 
// Function to find the sum of Bitwise AND
// of sum of pairs and their Bitwise AND
// from a given array
void findSum(int A[], int N)
{
    // Stores the total sum
    long long ans = 0;
 
    for (int i = 0; i < 30; i++) {
 
        vector<long long> vec;
 
        for (int j = 0; j < N; j++) {
 
            // Check if jth bit is set
            if ((A[j] >> i) & 1) {
 
                // Stores the right shifted
                // element by(i+1)
                long long X = (A[j] >> (i + 1));
 
                // Update the value
                // of X
                X = X * (1 << (i + 1));
                X %= M;
 
                // Push in vector vec
                vec.push_back(A[j] - X);
            }
        }
        // Sort the vector in
        // ascending order
        sort(vec.begin(), vec.end());
 
        // Traverse the vector vec
        for (int j = 0; j < vec.size(); j++) {
 
            // Stores the value
            // 2^(i+1)- 2^(i)- vec[j]
            int Y = (1 << (i + 1))
                    + (1 << i)
                    - vec[j];
 
            // Stores count of numbers
            // whose value > Y
            int idx = lower_bound(
                          vec.begin() + j + 1,
                          vec.end(), Y)
                      - vec.begin();
 
            // Update the ans
            ans += (vec.size() - idx)
                   * 1ll * (1 << i);
 
            ans %= M;
        }
    }
 
    // Return the ans
    cout << ans % M << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    findSum(arr, N);
 
    return 0;
}


Python3




# Python 3 program for the above approach
M = 1000000007
from bisect import bisect_left
 
# Function to find the sum of Bitwise AND
# of sum of pairs and their Bitwise AND
# from a given array
def findSum(A, N):
   
    # Stores the total sum
    ans = 0
 
    for i in range(30):
        vec = []
 
        for j in range(N):
            # Check if jth bit is set
            if ((A[j] >> i) & 1):
                # Stores the right shifted
                # element by(i+1)
                X = (A[j] >> (i + 1))
 
                # Update the value
                # of X
                X = X * (1 << (i + 1))
                X %= M
 
                # Push in vector vec
                vec.append(A[j] - X)
 
        # Sort the vector in
        # ascending order
        vec.sort(reverse=False)
 
        # Traverse the vector vec
        for j in range(len(vec)):
           
            # Stores the value
            # 2^(i+1)- 2^(i)- vec[j]
            Y = (1 << (i + 1)) + (1 << i) - vec[j]
 
            # Stores count of numbers
            # whose value > Y
            temp = vec[j+1:]
            idx = int(bisect_left(temp,Y))
 
            # Update the ans
            ans += ((len(vec) - idx) * (1 << i))
 
            ans %= M
    ans /= 7
 
    # Return the ans
    print(int(ans % M))
 
# Driver Code
if __name__ == '__main__':
    arr =  [1, 3, 3]
    N = len(arr)
    findSum(arr, N)
 
    # This code is contributed by SURENNDRA_GANGWAR.


Output: 

2

 

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :