Sum of Bitwise OR of every array element paired with all other array elements
Last Updated :
15 Nov, 2021
Given an array arr[] consisting of non-negative integers, the task for each array element arr[i] is to print the sum of Bitwise OR of all pairs (arr[i], arr[j]) ( 0 ≤ j ≤ N ).
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 12 14 16 22
Explanation:
For i = 0 the required sum will be (1 | 1) + (1 | 2) + (1 | 3) + (1 | 4) = 12
For i = 1 the required sum will be (2 | 1) + (2 | 2) + (2 | 3) + (2 | 4) = 14
For i = 2 the required sum will be (3 | 1) + (3 | 2) + (3 | 3) + (3 | 4) = 16
For i = 3 the required sum will be (4 | 1) + (4 | 2) + (4 | 3) + (4 | 4) = 22
Input: arr[] = {3, 2, 5, 4, 8}
Output: 31 28 37 34 54
Naive Approach: The simplest approach for every array element arr[i] is to traverse the array and calculate sum of Bitwise OR of all possible (arr[i], arr[j]) and print the obtained sum.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program of the above approach
// Function to print required sum for
// every valid index i
function prletORSumforEachElement(arr, N)
{
for(let i = 0; i < N; i++)
{
// Store the required sum
// for current array element
let req_sum = 0;
// Generate all possible pairs
// (arr[i], arr[j])
for(let j = 0; j < N; j++)
{
// Update the value of req_sum
req_sum += (arr[i] | arr[j]);
}
// Print the required sum
document.write(req_sum + " ");
}
}
// Driver Code
// Given array
let arr = [ 1, 2, 3, 4 ];
// Size of the array
let N = arr.length;
// Function Call
prletORSumforEachElement(arr, N);
// This code is contributed by avijitmondal1998
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is to use Bit Manipulation by using the assumption that integers are represented using 32 bits. Follow the steps below to solve the problem:
- Consider every k-th bit and use a frequency array freq[] to store the count of elements for which k-th bit is set.
- For every array element check whether k-th bit of that element is set or not. If k-th bit is set then simply increase the frequency of k-th bit.
- Traverse the array and for every element arr[i] check whether k-th bit of arr[i] is set or not.
- Initialize required sum to 0 for every index i.
- If k-th bit of arr[i] is set then it means, k-th bit of every possible (arr[i] | arr[j]) will also be set. So in this case add (1 << k) * N to the required sum.
- Otherwise, if k-th bit of arr[i] is not set then it means that k-th bit of (arr[i] | arr[j]) will be set if and only if k-th bit of arr[j] is set. So in this case add (1 << k) * freq[k] to the required sum which is previously calculated that k-th bit is set for freq[k] number of elements.
- Finally, print the value of required sum for index i.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program for the above approach
// Function to print required sum for
// every valid index i
function printORSumforEachElement(arr, N)
{
// Frequency array to store frequency
// of every k-th bit
var freq = Array(32).fill(0);
for(var i = 0; i < N; i++)
{
for(var 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(var i = 0; i < N; i++)
{
// Stores the required sum
// for the current array element
var req_sum = 0;
for(var 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
document.write(req_sum + " ");
}
}
// Driver Code
// Given array
var arr = [ 1, 2, 3, 4 ];
// Size of the array
var N = arr.length;
// Function Call
printORSumforEachElement(arr, N);
// This code is contributed by rutvik_56
</script>
Time Complexity: O(32 * N)
Auxiliary Space: O(m), where m = 32
Similar Reads
Sum of Bitwise XOR of each array element with all other array elements Given an array arr[] of length N, the task for every array element is to print the sum of its Bitwise XOR with all other array elements. Examples: Input: arr[] = {1, 2, 3}Output: 5 4 3Explanation:For arr[0]: arr[0] ^ arr[0] + arr[0] ^ arr[1] + arr[0] ^ arr[2] = 1^1 + 1^2 + 1^3 = 0 + 3 + 2 = 5For arr
9 min read
Sum of Bitwise XOR of elements of an array with all elements of another array Given an array arr[] of size N and an array Q[], the task is to calculate the sum of Bitwise XOR of all elements of the array arr[] with each element of the array q[]. Examples: Input: arr[ ] = {5, 2, 3}, Q[ ] = {3, 8, 7}Output: 7 34 11Explanation:For Q[0] ( = 3): Sum = 5 ^ 3 + 2 ^ 3 + 3 ^ 3 = 7.For
9 min read
Sum of Bitwise OR of each array element of an array with all elements of another array Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise OR of each element of arr1[] with every element of the array arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3Output: 7 8 9Explanation: For arr[0]: Sum = arr1[0]|arr2[0] + arr1[
11 min read
Sum of Bitwise AND of each array element with the elements of another array Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise AND of each element of arr1[] with the elements of the array arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3Output: 2 4 6Explanation:For elements at index 0 in arr1[], Sum = a
11 min read
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read