📌  相关文章
📜  一个数组的每个数组元素与另一个数组的所有元素的按位或之和

📅  最后修改于: 2021-09-07 02:04:41             🧑  作者: Mango

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

例子:

朴素的方法:解决这个问题的最简单的方法是遍历数组arr1[]并对数组arr[]中的每个数组元素,计算数组arr2[]中每个元素的按位或。

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

高效方法:对上述方法进行优化,思路是利用Bit Manipulation来解决上述问题。

  • 据该位或财产,执行操作时,第i位将设置当任两个数字都有在一组位 i位置,其中0 ≤ i <32
  • 因此,对于在ARR1 []的数,如果第i位不是一组位,则i位置将有助于K的总和* 2 i,其中K是在ARR2 []具有置位的位在总数第i位置。
  • 否则,如果数字在第i位置有一个设置位,那么它将贡献N * 2 i的总和。

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

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

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to compute sum of Bitwise OR
// of each element in arr1[] with all
// elements of the array arr2[]
void Bitwise_OR_sum_i(int arr1[], int arr2[],
                      int M, int N)
{
 
    // Declaring an array of
    // size 32 to store the
    // count of each bit
    int frequency[32] = { 0 };
 
    // Traverse the array arr1[]
    for (int i = 0; i < N; i++) {
 
        // Current bit position
        int bit_position = 0;
        int num = arr1[i];
 
        // While num exceeds 0
        while (num) {
 
            // Checks if i-th bit
            // is set or not
            if (num & 1) {
 
                // Increment the count at
                // bit_position by one
                frequency[bit_position] += 1;
            }
 
            // Increment bit_position
            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_OR_sum = 0;
 
        // Traverse in the range [0, 31]
        for (int bit_position = 0;
             bit_position < 32;
             bit_position++) {
 
            // Check if current bit is set
            if (num & 1) {
 
                // Increment the Bitwise
                // sum by N*(2^i)
                bitwise_OR_sum
                    += N * value_at_that_bit;
            }
            else {
                bitwise_OR_sum
                    += frequency[bit_position]
                       * value_at_that_bit;
            }
 
            // Right shift num by one
            num >>= 1;
 
            // Left shift valee_at_that_bit by one
            value_at_that_bit <<= 1;
        }
 
        // Print the sum obtained for ith
        // number in arr1[]
        cout << bitwise_OR_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_OR_sum_i(arr1, arr2, M, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to compute sum of Bitwise OR
// of each element in arr1[] with all
// elements of the array arr2[]
static void Bitwise_OR_sum_i(int arr1[], int arr2[],
                             int M, int N)
{
     
    // Declaring an array of
    // size 32 to store the
    // count of each bit
    int frequency[] = new int[32];
    Arrays.fill(frequency, 0);
  
    // Traverse the array arr1[]
    for(int i = 0; i < N; i++)
    {
         
        // Current bit position
        int bit_position = 0;
        int num = arr1[i];
  
        // While num exceeds 0
        while (num != 0)
        {
             
            // Checks if i-th bit
            // is set or not
            if ((num & 1) != 0)
            {
                 
                // Increment the count at
                // bit_position by one
                frequency[bit_position] += 1;
            }
  
            // Increment bit_position
            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_OR_sum = 0;
  
        // Traverse in the range [0, 31]
        for(int bit_position = 0;
                bit_position < 32;
                bit_position++)
        {
  
            // Check if current bit is set
            if ((num & 1) != 0)
            {
                 
                // Increment the Bitwise
                // sum by N*(2^i)
                bitwise_OR_sum += N * value_at_that_bit;
            }
            else
            {
                bitwise_OR_sum += frequency[bit_position] *
                                  value_at_that_bit;
            }
  
            // Right shift num by one
            num >>= 1;
  
            // Left shift valee_at_that_bit by one
            value_at_that_bit <<= 1;
        }
  
        // Print the sum obtained for ith
        // number in arr1[]
        System.out.print(bitwise_OR_sum + " ");
    }
    return;
}
  
// 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_OR_sum_i(arr1, arr2, M, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
  
# Function to compute sum of Bitwise OR
# of each element in arr1[] with all
# elements of the array arr2[]
def Bitwise_OR_sum_i(arr1, arr2, M, N):
  
    # Declaring an array of
    # size 32 to store the
    # count of each bit
    frequency = [0] * 32
  
    # Traverse the array arr1[]
    for i in range(N):
  
        # Current bit position
        bit_position = 0
        num = arr1[i]
  
        # While num exceeds 0
        while (num):
  
            # Checks if i-th bit
            # is set or not
            if (num & 1 != 0):
  
                # Increment the count at
                # bit_position by one
                frequency[bit_position] += 1
             
            # Increment bit_position
            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_OR_sum = 0
  
        # Traverse in the range [0, 31]
        for bit_position in range(32):
  
            # Check if current bit is set
            if (num & 1 != 0):
  
                # Increment the Bitwise
                # sum by N*(2^i)
                bitwise_OR_sum += N * value_at_that_bit
             
            else:
                bitwise_OR_sum += (frequency[bit_position] *
                                   value_at_that_bit)
             
            # Right shift num by one
            num >>= 1
  
            # Left shift valee_at_that_bit by one
            value_at_that_bit <<= 1
         
        # Print the sum obtained for ith
        # number in arr1[]
        print(bitwise_OR_sum, end = " ")
     
    return
 
# Driver Code
 
# 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_OR_sum_i(arr1, arr2, M, N)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
class GFG
{
      
// Function to compute sum of Bitwise OR
// of each element in arr1[] with all
// elements of the array arr2[]
static void Bitwise_OR_sum_i(int[] arr1, int[] arr2,
                             int M, int N)
{
      
    // Declaring an array of
    // size 32 to store the
    // count of each bit
    int[] frequency = new int[32];
    for(int i = 0; i < 32; i++)
    {
        frequency[i] = 0;
    }
 
    // Traverse the array arr1[]
    for(int i = 0; i < N; i++)
    {
          
        // Current bit position
        int bit_position = 0;
        int num = arr1[i];
   
        // While num exceeds 0
        while (num != 0)
        {
              
            // Checks if i-th bit
            // is set or not
            if ((num & 1) != 0)
            {
                  
                // Increment the count at
                // bit_position by one
                frequency[bit_position] += 1;
            }
   
            // Increment bit_position
            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_OR_sum = 0;
   
        // Traverse in the range [0, 31]
        for(int bit_position = 0;
                bit_position < 32;
                bit_position++)
        {
   
            // Check if current bit is set
            if ((num & 1) != 0)
            {
                  
                // Increment the Bitwise
                // sum by N*(2^i)
                bitwise_OR_sum += N * value_at_that_bit;
            }
            else
            {
                bitwise_OR_sum += frequency[bit_position] *
                                  value_at_that_bit;
            }
   
            // Right shift num by one
            num >>= 1;
   
            // Left shift valee_at_that_bit by one
            value_at_that_bit <<= 1;
        }
   
        // Print the sum obtained for ith
        // number in arr1[]
        Console.Write(bitwise_OR_sum + " ");
    }
    return;
}
  
// Driver Code
public static 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_OR_sum_i(arr1, arr2, M, N);
}
}
 
// This code is contributed by sanjoy_62


输出:
7 8 9

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live