📌  相关文章
📜  每个数组元素与另一个数组的元素按位与的总和

📅  最后修改于: 2021-05-17 02:18:45             🧑  作者: Mango

给定两个大小为M的数组arr1 []和大小为N的arr2 [] ,任务是找到arr1 []的每个元素与数组arr2 []的元素按位与的总和。

例子:

简单方法:解决该问题的最简单的方法是横动阵列ARR1 []和用于阵列ARR1 [中的每个元素],遍历数组ARR2 [],并计算按位,并与所有ARR1 []的当前元素的总和每个元素的arr2 []元素
时间复杂度: O(N 2 )
辅助空间: O(N)

高效的方法:想法是使用位操作来解决上述问题。假设数组的每个元素只能使用32位表示

  • 根据按位和财产执行操作时,第i位将被置位 仅当两个数字都有一个 设置在 i位置,其中0≤i<32 
  • 因此,对于在ARR1 []的数如果该i比特为一组,则i位置将有助于K的总和* 2 i,其中KARR2 []具有的比特总数 i位置。

请按照以下步骤解决问题:

  1. 初始化一个整数数组frequency []以将arr2 []中的位数存储在i位置( 0≤i<32)的位置
  2. 数组arr2 []中的遍历,对于每个元素,以二进制形式表示,并在二进制表示中具有1的位置,将frequency []数组中的计数递增1
  3. 遍历数组arr1 []
    1. 0初始化一个整数变量bitwise_AND_sum
    2. 使用变量j[0,31]范围内遍历。
    3. 如果在arr2 [i]的二进制表示形式中将第j位置1,则bitwise_AND_sum频率[j] * 2 j递增
    4. 打印获得的总和,即bitwise_AND_sum

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Fucntion to compute the AND sum
// for each element of an array
void Bitwise_AND_sum_i(int arr1[], int arr2[], int M, int N)
{
 
    // Declaring an array of
    // size 32 for storing the
    // count of each bit
    int frequency[32] = { 0 };
 
    // Traverse the array arr2[]
    // and store the count of a
    // bit in frequency array
    for (int i = 0; i < N; i++) {
 
        // Current bit position
        int bit_position = 0;
        int num = arr1[i];
 
        // While num is greater
        // than 0
        while (num) {
 
            // Checks if ith bit is
            // set or not
            if (num & 1) {
 
                // Increment the count of
                // bit by one
                frequency[bit_position] += 1;
            }
 
            // Increment the bit position
            // by one
            bit_position += 1;
 
            // Right shift the num by one
            num >>= 1;
        }
    }
 
    // Traverse in the arr2[]
    for (int i = 0; i < M; i++) {
 
        int num = arr2[i];
 
        // Store the ith bit
        // value
        int value_at_that_bit = 1;
 
        // Total required sum
        int bitwise_AND_sum = 0;
 
        // Traverse in the range [0, 31]
        for (int bit_position = 0; bit_position < 32;
             bit_position++) {
 
            // Checks if current bit is set
            if (num & 1) {
 
                // Incremen the bitwise sum
                // by frequency[bit_position]
                // * value_at_that_bit;
                bitwise_AND_sum += frequency[bit_position]
                                   * value_at_that_bit;
            }
 
            // Right shift num by one
            num >>= 1;
 
            // Left shift vale_at_that_bit by one
            value_at_that_bit <<= 1;
        }
 
        // Print the sum obtained for ith
        // number in arr1[]
        cout << bitwise_AND_sum << ' ';
    }
 
    return;
}
 
// Driver Code
int main()
{
 
    // Given arr1[]
    int arr1[] = { 1, 2, 3 };
 
    // Given arr2[]
    int arr2[] = { 1, 2, 3 };
 
    // Size of arr1[]
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Size of arr2[]
    int M = sizeof(arr2) / sizeof(arr2[0]);
 
    // Function Call
    Bitwise_AND_sum_i(arr1, arr2, M, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
      // Driver Code
    public static void main(String[] args)
    {
       
        // Given arr1[]
        int[] arr1 = { 1, 2, 3 };
 
        // Given arr2[]
        int[] arr2 = { 1, 2, 3 };
 
        // Size of arr1[]
        int N = arr1.length;
 
        // Size of arr2[]
        int M = arr2.length;
 
        // Function Call
        Bitwise_AND_sum_i(arr1, arr2, M, N);
    }
 
    // Fucntion to compute the AND sum
    // for each element of an array
    static void Bitwise_AND_sum_i(int arr1[], int arr2[],
                                  int M, int N)
    {
 
        // Declaring an array of
        // size 32 for storing the
        // count of each bit
        int[] frequency = new int[32];
 
        // Traverse the array arr2[]
        // and store the count of a
        // bit in frequency array
        for (int i = 0; i < N; i++)
        {
 
            // Current bit position
            int bit_position = 0;
            int num = arr1[i];
 
            // While num is greater
            // than 0
            while (num != 0)
            {
 
                // Checks if ith bit is
                // set or not
                if ((num & 1) != 0)
                {
 
                    // Increment the count of
                    // bit by one
                    frequency[bit_position] += 1;
                }
 
                // Increment the bit position
                // by one
                bit_position += 1;
 
                // Right shift the num by one
                num >>= 1;
            }
        }
 
        // Traverse in the arr2[]
        for (int i = 0; i < M; i++)
        {
            int num = arr2[i];
 
            // Store the ith bit
            // value
            int value_at_that_bit = 1;
 
            // Total required sum
            int bitwise_AND_sum = 0;
 
            // Traverse in the range [0, 31]
            for (int bit_position = 0; bit_position < 32;
                 bit_position++)
            {
 
                // Checks if current bit is set
                if ((num & 1) != 0)
                {
 
                    // Incremen the bitwise sum
                    // by frequency[bit_position]
                    // * value_at_that_bit;
                    bitwise_AND_sum
                        += frequency[bit_position]
                           * value_at_that_bit;
                }
 
                // Right shift num by one
                num >>= 1;
 
                // Left shift vale_at_that_bit by one
                value_at_that_bit <<= 1;
            }
 
            // Print the sum obtained for ith
            // number in arr1[]
            System.out.print( bitwise_AND_sum + " ");
        }
    }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Fucntion to compute the AND sum
# for each element of an array
def Bitwise_AND_sum_i(arr1, arr2, M, N):
 
    # Declaring an array of
    # size 32 for storing the
    # count of each bit
    frequency = [0]*32
 
    # Traverse the array arr2[]
    # and store the count of a
    # bit in frequency array
    for i in range(N):
 
        # Current bit position
        bit_position = 0
        num = arr1[i]
 
        # While num is greater
        # than 0
        while (num):
 
            # Checks if ith bit is
            # set or not
            if (num & 1):
 
                # Increment the count of
                # bit by one
                frequency[bit_position] += 1
 
            # Increment the bit position
            # by one
            bit_position += 1
 
            # Right shift the num by one
            num >>= 1
 
    # Traverse in the arr2[]
    for i in range(M):
        num = arr2[i]
 
        # Store the ith bit
        # value
        value_at_that_bit = 1
 
        # Total required sum
        bitwise_AND_sum = 0
 
        # Traverse in the range [0, 31]
        for bit_position in range(32):
 
            # Checks if current bit is set
            if (num & 1):
 
                # Incremen the bitwise sum
                # by frequency[bit_position]
                # * value_at_that_bit
                bitwise_AND_sum += frequency[bit_position] * value_at_that_bit
 
            # Right shift num by one
            num >>= 1
 
            # Left shift vale_at_that_bit by one
            value_at_that_bit <<= 1
 
        # Prthe sum obtained for ith
        # number in arr1[]
        print(bitwise_AND_sum, end = " ")
    return
 
# Driver Code
if __name__ == '__main__':
 
    # Given arr1[]
    arr1 = [1, 2, 3]
 
    # Given arr2
    arr2 = [1, 2, 3]
 
    # Size of arr1[]
    N = len(arr1)
 
    # Size of arr2[]
    M = len(arr2)
 
    # Function Call
    Bitwise_AND_sum_i(arr1, arr2, M, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
      // Driver code
    static public void Main()
    {
 
        // Given arr1[]
        int[] arr1 = { 1, 2, 3 };
 
        // Given arr2[]
        int[] arr2 = { 1, 2, 3 };
 
        // Size of arr1[]
        int N = arr1.Length;
 
        // Size of arr2[]
        int M = arr2.Length;
 
        // Function Call
        Bitwise_AND_sum_i(arr1, arr2, M, N);
    }
 
    // Fucntion to compute the AND sum
    // for each element of an array
    static void Bitwise_AND_sum_i(int[] arr1, int[] arr2,
                                  int M, int N)
    {
 
        // Declaring an array of
        // size 32 for storing the
        // count of each bit
        int[] frequency = new int[32];
 
        // Traverse the array arr2[]
        // and store the count of a
        // bit in frequency array
        for (int i = 0; i < N; i++)
        {
 
            // Current bit position
            int bit_position = 0;
            int num = arr1[i];
 
            // While num is greater
            // than 0
            while (num != 0)
            {
 
                // Checks if ith bit is
                // set or not
                if ((num & 1) != 0)
                {
 
                    // Increment the count of
                    // bit by one
                    frequency[bit_position] += 1;
                }
 
                // Increment the bit position
                // by one
                bit_position += 1;
 
                // Right shift the num by one
                num >>= 1;
            }
        }
 
        // Traverse in the arr2[]
        for (int i = 0; i < M; i++)
        {
 
            int num = arr2[i];
 
            // Store the ith bit
            // value
            int value_at_that_bit = 1;
 
            // Total required sum
            int bitwise_AND_sum = 0;
 
            // Traverse in the range [0, 31]
            for (int bit_position = 0; bit_position < 32;
                 bit_position++) {
 
                // Checks if current bit is set
                if ((num & 1) != 0)
                {
 
                    // Incremen the bitwise sum
                    // by frequency[bit_position]
                    // * value_at_that_bit;
                    bitwise_AND_sum
                        += frequency[bit_position]
                           * value_at_that_bit;
                }
 
                // Right shift num by one
                num >>= 1;
 
                // Left shift vale_at_that_bit by one
                value_at_that_bit <<= 1;
            }
 
            // Print the sum obtained for ith
            // number in arr1[]
            Console.Write(bitwise_AND_sum + " ");
        }
    }
}
 
// The code is contributed by Dharanendra L V


输出:
2 4 6

时间复杂度: O(N * 32)
辅助空间: O(N * 32)