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

📅  最后修改于: 2021-04-22 08:23:14             🧑  作者: Mango

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

例子:

天真的方法:对于每个数组元素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


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.


输出:
12 14 16 22

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

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

  1. 考虑每个第k位,并使用频率数组freq []存储设置了第k位的元素的计数
  2. 对于每个数组元素,检查是否设置了该元素的第k位。如果设置了第k位,则只需增加第k位的频率即可。
  3. 横移 的阵列和用于每个元素ARR [I]确认第k ARR的位[I]是否被设定或没有。
  4. 将每个索引i的所需总和初始化为0
  5. 如果arr [i]的第k位被置位,则意味着每个可能的第k(arr [i] | arr [j])也将被置位。因此,在这种情况下,将(1 << k)* N添加到所需的总和中
  6. 否则,如果没有设置ARR [I]k个比特,则意味着第k个(ARR [I] |的常用3 [j]的)将被设置当且仅当ARR第k[J ]设置。因此,在这种情况下,将(1 << k)* freq [k]添加到所需的总和,该总和是先前计算出的第k位被设置为freq [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

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.
输出:
12 14 16 22

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