📌  相关文章
📜  每个数组元素与所有其他数组元素配对的按位或之和

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

给定的阵列ARR []由非负整数的,对于每个阵列元素ARR任务[i]是打印按位或所有对的总和(ARR [I],编曲[j]的)(0≤Ĵ≤)。

例子:

朴素的方法:对于每个数组元素arr[i]最简单的方法是遍历数组并计算所有可能(arr[i], arr[j])按位或总和并打印获得的总和

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print required sum for
// every valid index i
void printORSumforEachElement(int arr[], int N)
{
    for (int i = 0; i < N; i++) {
 
        // Store the required sum
        // for current array element
        int req_sum = 0;
 
        // Generate all possible pairs (arr[i], arr[j])
        for (int j = 0; j < N; j++) {
 
            // Update the value of req_sum
            req_sum += (arr[i] | arr[j]);
        }
 
        // Print the required sum
        cout << req_sum << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    printORSumforEachElement(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to print required sum for
// every valid index i
static void printORSumforEachElement(int arr[], int N)
{
    for (int i = 0; i < N; i++)
    {
 
        // Store the required sum
        // for current array element
        int req_sum = 0;
 
        // Generate all possible pairs (arr[i], arr[j])
        for (int j = 0; j < N; j++)
        {
 
            // Update the value of req_sum
            req_sum += (arr[i] | arr[j]);
        }
 
        // Print the required sum
        System.out.print(req_sum+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    printORSumforEachElement(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to print required sum for
# every valid index i
def printORSumforEachElement(arr, N):
     
    for i in range(0, N):
         
        # Store the required sum
        # for current array element
        req_sum = 0
         
        # Generate all possible pairs(arr[i],arr[j])
        for j in range(0, N):
             
            # Update the value of req_sum
            req_sum += (arr[i] | arr[j])
             
        # Print required sum
        print(req_sum, end = " ")
 
# Driver code
 
# Given array
arr = [ 1, 2, 3, 4 ]
 
# Size of array
N = len(arr)
 
# Function call
printORSumforEachElement(arr, N)
 
# This code is contributed by Virusbuddah


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to print required sum for
// every valid index i
static void printORSumforEachElement(int []arr, int N)
{
    for (int i = 0; i < N; i++)
    {
 
        // Store the required sum
        // for current array element
        int req_sum = 0;
 
        // Generate all possible pairs (arr[i], arr[j])
        for (int j = 0; j < N; j++)
        {
 
            // Update the value of req_sum
            req_sum += (arr[i] | arr[j]);
        }
 
        // Print the required sum
        Console.Write(req_sum+ " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given array
    int []arr = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function Call
    printORSumforEachElement(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print required sum for
// every valid index i
void printORSumforEachElement(int arr[], int N)
{
    // Frequency array to store frequency
    // of every k-th bit
    int freq[32];
 
    // Initialize frequency array
    memset(freq, 0, sizeof freq);
 
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < 32; k++) {
 
            // If k-th bit is set, then update
            // the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0)
                freq[k]++;
        }
    }
 
    for (int i = 0; i < N; i++) {
 
        // Stores the required sum
        // for the current array element
        int req_sum = 0;
 
        for (int k = 0; k < 32; k++) {
 
            // If k-th bit is set
            if ((arr[i] & (1 << k)) != 0)
                req_sum += (1 << k) * N;
 
            // If k-th bit is not set
            else
                req_sum += (1 << k) * freq[k];
        }
 
        // Print the required sum
        cout << req_sum << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    printORSumforEachElement(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to print required sum for
// every valid index i
static void printORSumforEachElement(int arr[], int N)
{
   
    // Frequency array to store frequency
    // of every k-th bit
    int []freq = new int[32];
    for (int i = 0; i < N; i++)
    {
        for (int k = 0; k < 32; k++)
        {
 
            // If k-th bit is set, then update
            // the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0)
                freq[k]++;
        }
    }
    for (int i = 0; i < N; i++)
    {
 
        // Stores the required sum
        // for the current array element
        int req_sum = 0;
        for (int k = 0; k < 32; k++)
        {
 
            // If k-th bit is set
            if ((arr[i] & (1 << k)) != 0)
                req_sum += (1 << k) * N;
 
            // If k-th bit is not set
            else
                req_sum += (1 << k) * freq[k];
        }
 
        // Print the required sum
        System.out.print(req_sum+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    printORSumforEachElement(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to print required sum for
# every valid index i
def printORSumforEachElement(arr, N):
     
    # Frequency array to store frequency
    # of every k-th bit
    freq = [0 for i in range(32)]
 
    for i in range(N):
        for k in  range(32):
             
            # If k-th bit is set, then update
            # the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0):
                freq[k] += 1
 
    for i in range(N):
         
        # Stores the required sum
        # for the current array element
        req_sum = 0
 
        for k in range(32):
             
            # If k-th bit is set
            if ((arr[i] & (1 << k)) != 0):
                req_sum += (1 << k) * N
 
            # If k-th bit is not set
            else:
                req_sum += (1 << k) * freq[k]
 
        # Print the required sum
        print(req_sum, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 1, 2, 3, 4 ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    printORSumforEachElement(arr, N)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to print required sum for
    // every valid index i
    static void printORSumforEachElement(int[] arr, int N)
    {
       
        // Frequency array to store frequency
        // of every k-th bit
        int[] freq = new int[32];
 
        for (int i = 0; i < N; i++)
        {
            for (int k = 0; k < 32; k++)
            {
 
                // If k-th bit is set, then update
                // the frequency of k-th bit
                if ((arr[i] & (1 << k)) != 0)
                    freq[k]++;
            }
        }
 
        for (int i = 0; i < N; i++)
        {
 
            // Stores the required sum
            // for the current array element
            int req_sum = 0;
            for (int k = 0; k < 32; k++)
            {
 
                // If k-th bit is set
                if ((arr[i] & (1 << k)) != 0)
                    req_sum += (1 << k) * N;
 
                // If k-th bit is not set
                else
                    req_sum += (1 << k) * freq[k];
            }
 
            // Print the required sum
            Console.Write(req_sum + " ");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        // Given array
        int[] arr = { 1, 2, 3, 4 };
 
        // Size of the array
        int N = arr.Length;
 
        // Function Call
        printORSumforEachElement(arr, N);
    }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
12 14 16 22

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

高效方法:最佳想法是通过假设整数使用32表示来使用位操作。请按照以下步骤解决问题:

  1. 考虑每个第k位并使用频率数组freq[]来存储设置了第 k 位的元素的计数
  2. 对于每个数组元素,检查是否设置了该元素的第 k 位。如果设置了第 k位,则只需增加第k位的频率。
  3. 遍历 数组和每个元素arr[i]检查是否设置了arr[i]的第 k位。
  4. 将每个索引i 的所需总和初始化为0
  5. 如果设置了arr[i]的第 k位,则意味着也将设置每个可能的(arr[i] | arr[j])的第 k位。所以在这种情况下,将(1 << k) * N 添加到所需的sum
  6. 否则,如果没有设置ARR [I]k个比特,则意味着第k个(ARR [I] |的常用3 [j]的)将被设置当且仅当ARR第k[J ]设置。因此,在这种情况下,将(1 << k) * freq[k] 添加到所需的总和中,该总和是先前计算的,为freq[k]数量的元素设置了第 k位。
  7. 最后,打印索引i所需总和的值。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print required sum for
// every valid index i
void printORSumforEachElement(int arr[], int N)
{
    // Frequency array to store frequency
    // of every k-th bit
    int freq[32];
 
    // Initialize frequency array
    memset(freq, 0, sizeof freq);
 
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < 32; k++) {
 
            // If k-th bit is set, then update
            // the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0)
                freq[k]++;
        }
    }
 
    for (int i = 0; i < N; i++) {
 
        // Stores the required sum
        // for the current array element
        int req_sum = 0;
 
        for (int k = 0; k < 32; k++) {
 
            // If k-th bit is set
            if ((arr[i] & (1 << k)) != 0)
                req_sum += (1 << k) * N;
 
            // If k-th bit is not set
            else
                req_sum += (1 << k) * freq[k];
        }
 
        // Print the required sum
        cout << req_sum << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    printORSumforEachElement(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to print required sum for
// every valid index i
static void printORSumforEachElement(int arr[], int N)
{
   
    // Frequency array to store frequency
    // of every k-th bit
    int []freq = new int[32];
    for (int i = 0; i < N; i++)
    {
        for (int k = 0; k < 32; k++)
        {
 
            // If k-th bit is set, then update
            // the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0)
                freq[k]++;
        }
    }
    for (int i = 0; i < N; i++)
    {
 
        // Stores the required sum
        // for the current array element
        int req_sum = 0;
        for (int k = 0; k < 32; k++)
        {
 
            // If k-th bit is set
            if ((arr[i] & (1 << k)) != 0)
                req_sum += (1 << k) * N;
 
            // If k-th bit is not set
            else
                req_sum += (1 << k) * freq[k];
        }
 
        // Print the required sum
        System.out.print(req_sum+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given array
    int arr[] = { 1, 2, 3, 4 };
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    printORSumforEachElement(arr, N);
}
}
 
// This code is contributed by shikhasingrajput

蟒蛇3

# Python3 program for the above approach
 
# Function to print required sum for
# every valid index i
def printORSumforEachElement(arr, N):
     
    # Frequency array to store frequency
    # of every k-th bit
    freq = [0 for i in range(32)]
 
    for i in range(N):
        for k in  range(32):
             
            # If k-th bit is set, then update
            # the frequency of k-th bit
            if ((arr[i] & (1 << k)) != 0):
                freq[k] += 1
 
    for i in range(N):
         
        # Stores the required sum
        # for the current array element
        req_sum = 0
 
        for k in range(32):
             
            # If k-th bit is set
            if ((arr[i] & (1 << k)) != 0):
                req_sum += (1 << k) * N
 
            # If k-th bit is not set
            else:
                req_sum += (1 << k) * freq[k]
 
        # Print the required sum
        print(req_sum, end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 1, 2, 3, 4 ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    printORSumforEachElement(arr, N)
     
# This code is contributed by SURENDRA_GANGWAR

C#

// C# program for the above approach
using System;
class GFG
{
   
    // Function to print required sum for
    // every valid index i
    static void printORSumforEachElement(int[] arr, int N)
    {
       
        // Frequency array to store frequency
        // of every k-th bit
        int[] freq = new int[32];
 
        for (int i = 0; i < N; i++)
        {
            for (int k = 0; k < 32; k++)
            {
 
                // If k-th bit is set, then update
                // the frequency of k-th bit
                if ((arr[i] & (1 << k)) != 0)
                    freq[k]++;
            }
        }
 
        for (int i = 0; i < N; i++)
        {
 
            // Stores the required sum
            // for the current array element
            int req_sum = 0;
            for (int k = 0; k < 32; k++)
            {
 
                // If k-th bit is set
                if ((arr[i] & (1 << k)) != 0)
                    req_sum += (1 << k) * N;
 
                // If k-th bit is not set
                else
                    req_sum += (1 << k) * freq[k];
            }
 
            // Print the required sum
            Console.Write(req_sum + " ");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        // Given array
        int[] arr = { 1, 2, 3, 4 };
 
        // Size of the array
        int N = arr.Length;
 
        // Function Call
        printORSumforEachElement(arr, N);
    }
}
 
// This code is contributed by chitranayal.

Javascript


输出:
12 14 16 22

时间复杂度: O(32 * N)
辅助空间: O(m),其中 m = 32

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